OpenShot Audio Library | OpenShotAudio  0.3.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
static StringArray fromLines(StringRef stringToBreakUp)
int size() const noexcept
void set(const String &key, const String &value)
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
bool isNotEmpty() const noexcept
Definition: juce_String.h:302
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
WebInputStream & withConnectionTimeout(int timeoutInMs)
WebInputStream & withExtraHeaders(const String &extraHeaders)
bool setPosition(int64 wantedPos) override
WebInputStream & withNumRedirectsToFollow(int numRedirects)
int64 getTotalLength() override
bool connect(Listener *listener)
StringPairArray getRequestHeaders() const
WebInputStream & withCustomRequestCommand(const String &customRequestCommand)
int read(void *destBuffer, int maxBytesToRead) override
WebInputStream(const URL &url, const bool usePost)
StringPairArray getResponseHeaders()
int64 getPosition() override