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 
23 namespace juce
24 {
25 
26 WebInputStream::WebInputStream (const URL& url, const bool usePost)
27  : pimpl (new Pimpl (*this, url, usePost)), hasCalledConnect (false)
28 {
29 }
30 
31 WebInputStream::~WebInputStream()
32 {
33  delete pimpl;
34 }
35 
36 WebInputStream& WebInputStream::withExtraHeaders (const String& extra) { pimpl->withExtraHeaders (extra); return *this; }
37 WebInputStream& WebInputStream::withCustomRequestCommand (const String& cmd) { pimpl->withCustomRequestCommand(cmd); return *this; }
38 WebInputStream& WebInputStream::withConnectionTimeout (int t) { pimpl->withConnectionTimeout (t); return *this; }
39 WebInputStream& WebInputStream::withNumRedirectsToFollow (int num) { pimpl->withNumRedirectsToFollow (num); return *this; }
40 StringPairArray WebInputStream::getRequestHeaders() const { return pimpl->getRequestHeaders(); }
41 StringPairArray WebInputStream::getResponseHeaders() { connect (nullptr); return pimpl->getResponseHeaders(); }
42 bool WebInputStream::isError() const { return pimpl->isError(); }
43 void WebInputStream::cancel() { pimpl->cancel(); }
44 bool WebInputStream::isExhausted() { return pimpl->isExhausted(); }
45 int64 WebInputStream::getPosition() { return pimpl->getPosition(); }
46 int64 WebInputStream::getTotalLength() { connect (nullptr); return pimpl->getTotalLength(); }
47 int WebInputStream::read (void* buffer, int bytes) { connect (nullptr); return pimpl->read (buffer, bytes); }
48 bool WebInputStream::setPosition (int64 pos) { return pimpl->setPosition (pos); }
49 int 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 
60 StringPairArray 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 
82 void WebInputStream::createHeadersAndPostData (const URL& aURL, String& headers, MemoryBlock& data)
83 {
84  aURL.createHeadersAndPostData (headers, data);
85 }
86 
87 } // namespace juce
juce::WebInputStream::cancel
void cancel()
Will cancel a blocking read and prevent any subsequent connection attempts.
Definition: juce_WebInputStream.cpp:43
juce::StringArray
A special array for holding a list of strings.
Definition: juce_StringArray.h:38
juce::WebInputStream::connect
bool connect(Listener *listener)
Wait until the first byte is ready for reading.
Definition: juce_WebInputStream.cpp:51
juce::WebInputStream
An InputStream which can be used to read from a given url.
Definition: juce_WebInputStream.h:36
juce::WebInputStream::getResponseHeaders
StringPairArray getResponseHeaders()
Returns a string array pair of response headers.
Definition: juce_WebInputStream.cpp:41
juce::URL
Represents a URL and has a bunch of useful functions to manipulate it.
Definition: juce_URL.h:41
juce::WebInputStream::Listener
Used to receive callbacks for data send progress.
Definition: juce_WebInputStream.h:40
juce::WebInputStream::WebInputStream
WebInputStream(const URL &url, const bool usePost)
Creates a new WebInputstream which can be used to read from a url.
Definition: juce_WebInputStream.cpp:26
juce::WebInputStream::getTotalLength
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
Definition: juce_WebInputStream.cpp:46
juce::WebInputStream::read
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
Definition: juce_WebInputStream.cpp:47
juce::WebInputStream::isExhausted
bool isExhausted() override
Returns true if the stream has no more data to read.
Definition: juce_WebInputStream.cpp:44
juce::WebInputStream::withExtraHeaders
WebInputStream & withExtraHeaders(const String &extraHeaders)
Add extra headers to http request.
Definition: juce_WebInputStream.cpp:36
juce::StringArray::size
int size() const noexcept
Returns the number of strings in the array.
Definition: juce_StringArray.h:139
juce::StringArray::fromLines
static StringArray fromLines(StringRef stringToBreakUp)
Returns an array containing the lines in a given string.
Definition: juce_StringArray.cpp:402
juce::StringPairArray
A container for holding a set of strings which are keyed by another string.
Definition: juce_StringPairArray.h:38
juce::WebInputStream::getStatusCode
int getStatusCode()
Returns the status code returned by the http server.
Definition: juce_WebInputStream.cpp:49
juce::String::upToFirstOccurrenceOf
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the first occurrence of a substring.
Definition: juce_String.cpp:1591
juce::WebInputStream::withCustomRequestCommand
WebInputStream & withCustomRequestCommand(const String &customRequestCommand)
Override the http command that is sent.
Definition: juce_WebInputStream.cpp:37
juce::WebInputStream::setPosition
bool setPosition(int64 wantedPos) override
Tries to move the current read position of the stream.
Definition: juce_WebInputStream.cpp:48
juce::StringPairArray::set
void set(const String &key, const String &value)
Adds or amends a key/value pair.
Definition: juce_StringPairArray.cpp:105
juce::WebInputStream::withConnectionTimeout
WebInputStream & withConnectionTimeout(int timeoutInMs)
Specify the connection time-out.
Definition: juce_WebInputStream.cpp:38
juce::WebInputStream::getPosition
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.
Definition: juce_WebInputStream.cpp:45
juce::WebInputStream::getRequestHeaders
StringPairArray getRequestHeaders() const
Returns a string array pair of the request headers.
Definition: juce_WebInputStream.cpp:40
juce::String::isNotEmpty
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Definition: juce_String.h:306
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::String::fromFirstOccurrenceOf
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from a given substring.
Definition: juce_String.cpp:1571
juce::WebInputStream::isError
bool isError() const
Returns true if there was an error during the connection attempt.
Definition: juce_WebInputStream.cpp:42
juce::WebInputStream::withNumRedirectsToFollow
WebInputStream & withNumRedirectsToFollow(int numRedirects)
Specify the number of redirects to be followed.
Definition: juce_WebInputStream.cpp:39