OpenShot Library | libopenshot-audio  0.1.9
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
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.
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from a given substring.
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
bool isError() const
Returns true if there was an error during the connection attempt.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Definition: juce_String.h:306
bool setPosition(int64 wantedPos) override
Tries to move the current read position of the stream.
int getStatusCode()
Returns the status code returned by the http server.
StringPairArray getRequestHeaders() const
Returns a string array pair of the request headers.
A special array for holding a list of strings.
The JUCE String class!
Definition: juce_String.h:42
An InputStream which can be used to read from a given url.
WebInputStream & withNumRedirectsToFollow(int numRedirects)
Specify the number of redirects to be followed.
WebInputStream & withConnectionTimeout(int timeoutInMs)
Specify the connection time-out.
void cancel()
Will cancel a blocking read and prevent any subsequent connection attempts.
WebInputStream(const URL &url, const bool usePost)
Creates a new WebInputstream which can be used to read from a url.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the first occurrence of a substring.
static StringArray fromLines(StringRef stringToBreakUp)
Returns an array containing the lines in a given string.
WebInputStream & withExtraHeaders(const String &extraHeaders)
Add extra headers to http request.
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.
WebInputStream & withCustomRequestCommand(const String &customRequestCommand)
Override the http command that is sent.
A class to hold a resizable block of raw data.
Used to receive callbacks for data send progress.
bool isExhausted() override
Returns true if the stream has no more data to read.
bool connect(Listener *listener)
Wait until the first byte is ready for reading.
Represents a URL and has a bunch of useful functions to manipulate it.
Definition: juce_URL.h:41