OpenShot Library | libopenshot-audio 0.2.0
juce_WebInputStream.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26WebInputStream::WebInputStream (const URL& url, const bool usePost)
27 : pimpl (new Pimpl (*this, url, usePost)), hasCalledConnect (false)
28{
29}
30
31WebInputStream::~WebInputStream()
32{
33 delete pimpl;
34}
35
36WebInputStream& WebInputStream::withExtraHeaders (const String& extra) { pimpl->withExtraHeaders (extra); return *this; }
37WebInputStream& WebInputStream::withCustomRequestCommand (const String& cmd) { pimpl->withCustomRequestCommand(cmd); return *this; }
38WebInputStream& WebInputStream::withConnectionTimeout (int t) { pimpl->withConnectionTimeout (t); return *this; }
39WebInputStream& WebInputStream::withNumRedirectsToFollow (int num) { pimpl->withNumRedirectsToFollow (num); return *this; }
40StringPairArray WebInputStream::getRequestHeaders() const { return pimpl->getRequestHeaders(); }
41StringPairArray WebInputStream::getResponseHeaders() { connect (nullptr); return pimpl->getResponseHeaders(); }
42bool WebInputStream::isError() const { return pimpl->isError(); }
43void WebInputStream::cancel() { pimpl->cancel(); }
44bool WebInputStream::isExhausted() { return pimpl->isExhausted(); }
45int64 WebInputStream::getPosition() { return pimpl->getPosition(); }
46int64 WebInputStream::getTotalLength() { connect (nullptr); return pimpl->getTotalLength(); }
47int WebInputStream::read (void* buffer, int bytes) { connect (nullptr); return pimpl->read (buffer, bytes); }
48bool WebInputStream::setPosition (int64 pos) { return pimpl->setPosition (pos); }
49int WebInputStream::getStatusCode() { connect (nullptr); return pimpl->getStatusCode(); }
50
52{
53 if (hasCalledConnect)
54 return ! isError();
55
56 hasCalledConnect = true;
57 return pimpl->connect (listener);
58}
59
60StringPairArray WebInputStream::parseHttpHeaders (const String& headerData)
61{
62 StringPairArray headerPairs;
63 StringArray headerLines = StringArray::fromLines (headerData);
64
65 // ignore the first line as this is the status line
66 for (int i = 1; i < headerLines.size(); ++i)
67 {
68 const String& headersEntry = headerLines[i];
69
70 if (headersEntry.isNotEmpty())
71 {
72 const String key (headersEntry.upToFirstOccurrenceOf (": ", false, false));
73 const String value (headersEntry.fromFirstOccurrenceOf (": ", false, false));
74 const String previousValue (headerPairs [key]);
75 headerPairs.set (key, previousValue.isEmpty() ? value : (previousValue + "," + value));
76 }
77 }
78
79 return headerPairs;
80}
81
82void WebInputStream::createHeadersAndPostData (const URL& aURL, String& headers, MemoryBlock& data)
83{
84 aURL.createHeadersAndPostData (headers, data);
85}
86
87} // namespace juce
A special array for holding a list of strings.
static StringArray fromLines(StringRef stringToBreakUp)
Returns an array containing the lines in a given string.
int size() const noexcept
Returns the number of strings in the array.
A container for holding a set of strings which are keyed by another string.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
The JUCE String class!
Definition juce_String.h:43
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the first occurrence of a substring.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from a given substring.
Represents a URL and has a bunch of useful functions to manipulate it.
Definition juce_URL.h:42
Used to receive callbacks for data send progress.
An InputStream which can be used to read from a given url.
WebInputStream & withConnectionTimeout(int timeoutInMs)
Specify the connection time-out.
WebInputStream & withExtraHeaders(const String &extraHeaders)
Add extra headers to http request.
bool setPosition(int64 wantedPos) override
Tries to move the current read position of the stream.
WebInputStream & withNumRedirectsToFollow(int numRedirects)
Specify the number of redirects to be followed.
int getStatusCode()
Returns the status code returned by the http server.
bool isExhausted() override
Returns true if the stream has no more data to read.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
bool connect(Listener *listener)
Wait until the first byte is ready for reading.
StringPairArray getRequestHeaders() const
Returns a string array pair of the request headers.
WebInputStream & withCustomRequestCommand(const String &customRequestCommand)
Override the http command that is sent.
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
WebInputStream(const URL &url, const bool usePost)
Creates a new WebInputstream which can be used to read from a url.
bool isError() const
Returns true if there was an error during the connection attempt.
void cancel()
Will cancel a blocking read and prevent any subsequent connection attempts.
StringPairArray getResponseHeaders()
Returns a string array pair of response headers.
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.