OpenShot Audio Library | OpenShotAudio  0.6.0
juce_URL.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
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 class WebInputStream;
27 
28 //==============================================================================
37 class JUCE_API URL
38 {
39 public:
40  //==============================================================================
42  URL();
43 
50  URL (const String& url);
51 
53  explicit URL (File localFile);
54 
60  bool operator== (const URL&) const;
61  bool operator!= (const URL&) const;
62 
63  //==============================================================================
72  String toString (bool includeGetParameters) const;
73 
75  bool isEmpty() const noexcept;
76 
78  bool isWellFormed() const;
79 
84  String getDomain() const;
85 
96  String getSubPath (bool includeGetParameters = false) const;
97 
101  String getQueryString() const;
102 
106  String getAnchorString() const;
107 
113  String getScheme() const;
114 
116  bool isLocalFile() const;
117 
127  File getLocalFile() const;
128 
137  String getFileName() const;
138 
143  int getPort() const;
144 
152  [[nodiscard]] URL withNewDomainAndPath (const String& newFullPath) const;
153 
161  [[nodiscard]] URL withNewSubPath (const String& newPath) const;
162 
167  URL getParentURL() const;
168 
181  URL getChildURL (const String& subPath) const;
182 
183  //==============================================================================
194  [[nodiscard]] URL withParameter (const String& parameterName,
195  const String& parameterValue) const;
196 
203  [[nodiscard]] URL withParameters (const StringPairArray& parametersToAdd) const;
204 
207  [[nodiscard]] URL withAnchor (const String& anchor) const;
208 
220  [[nodiscard]] URL withFileToUpload (const String& parameterName,
221  const File& fileToUpload,
222  const String& mimeType) const;
223 
234  [[nodiscard]] URL withDataToUpload (const String& parameterName,
235  const String& filename,
236  const MemoryBlock& fileContentToUpload,
237  const String& mimeType) const;
238 
249  const StringArray& getParameterNames() const noexcept { return parameterNames; }
250 
263  const StringArray& getParameterValues() const noexcept { return parameterValues; }
264 
273  [[nodiscard]] URL withPOSTData (const String& postData) const;
274 
283  [[nodiscard]] URL withPOSTData (const MemoryBlock& postData) const;
284 
286  String getPostData() const { return postData.toString(); }
287 
289  const MemoryBlock& getPostDataAsMemoryBlock() const noexcept { return postData; }
290 
291  //==============================================================================
296  bool launchInDefaultBrowser() const;
297 
298  //==============================================================================
302  static bool isProbablyAWebsiteURL (const String& possibleURL);
303 
307  static bool isProbablyAnEmailAddress (const String& possibleEmailAddress);
308 
309  //==============================================================================
310  enum class ParameterHandling
311  {
312  inAddress,
313  inPostData
314  };
315 
330  class JUCE_API InputStreamOptions
331  {
332  public:
339  explicit InputStreamOptions (ParameterHandling parameterHandling);
340 
341  //==============================================================================
346  [[nodiscard]] InputStreamOptions withProgressCallback (std::function<bool (int bytesSent, int totalBytes)> progressCallback) const;
347 
352  [[nodiscard]] InputStreamOptions withExtraHeaders (const String& extraHeaders) const;
353 
359  [[nodiscard]] InputStreamOptions withConnectionTimeoutMs (int connectionTimeoutMs) const;
360 
364  [[nodiscard]] InputStreamOptions withResponseHeaders (StringPairArray* responseHeaders) const;
365 
369  [[nodiscard]] InputStreamOptions withStatusCode (int* statusCode) const;
370 
375  [[nodiscard]] InputStreamOptions withNumRedirectsToFollow (int numRedirectsToFollow) const;
376 
384  [[nodiscard]] InputStreamOptions withHttpRequestCmd (const String& httpRequestCmd) const;
385 
386  //==============================================================================
387  ParameterHandling getParameterHandling() const noexcept { return parameterHandling; }
388  std::function<bool (int, int)> getProgressCallback() const noexcept { return progressCallback; }
389  String getExtraHeaders() const noexcept { return extraHeaders; }
390  int getConnectionTimeoutMs() const noexcept { return connectionTimeOutMs; }
391  StringPairArray* getResponseHeaders() const noexcept { return responseHeaders; }
392  int* getStatusCode() const noexcept { return statusCode; }
393  int getNumRedirectsToFollow() const noexcept { return numRedirectsToFollow; }
394  String getHttpRequestCmd() const noexcept { return httpRequestCmd; }
395 
396  private:
397  //==============================================================================
398  const ParameterHandling parameterHandling;
399 
400  std::function<bool (int, int)> progressCallback = nullptr;
401  String extraHeaders;
402  int connectionTimeOutMs = 0;
403  StringPairArray* responseHeaders = nullptr;
404  int* statusCode = nullptr;
405  int numRedirectsToFollow = 5;
406  String httpRequestCmd;
407  };
408 
427  std::unique_ptr<InputStream> createInputStream (const InputStreamOptions& options) const;
428 
434  std::unique_ptr<OutputStream> createOutputStream() const;
435 
436  //==============================================================================
437  class DownloadTask;
438 
440  struct JUCE_API DownloadTaskListener
441  {
442  virtual ~DownloadTaskListener() = default;
443 
447  virtual void finished (DownloadTask* task, bool success) = 0;
448 
453  virtual void progress (DownloadTask* task, int64 bytesDownloaded, int64 totalLength);
454  };
455 
460  {
461  public:
462  String extraHeaders;
463  String sharedContainer;
464  DownloadTaskListener* listener = nullptr;
465  bool usePost = false;
466 
468  [[nodiscard]] auto withExtraHeaders (String value) const { return with (&DownloadTaskOptions::extraHeaders, std::move (value)); }
469 
477  [[nodiscard]] auto withSharedContainer (String value) const { return with (&DownloadTaskOptions::sharedContainer, std::move (value)); }
478 
480  [[nodiscard]] auto withListener (DownloadTaskListener* value) const { return with (&DownloadTaskOptions::listener, std::move (value)); }
481 
483  [[nodiscard]] auto withUsePost (bool value) const { return with (&DownloadTaskOptions::usePost, value); }
484 
485  private:
486  template <typename Member, typename Value>
487  [[nodiscard]] DownloadTaskOptions with (Member&& member, Value&& value) const
488  {
489  auto copy = *this;
490  copy.*member = std::forward<Value> (value);
491  return copy;
492  }
493  };
494 
499  class JUCE_API DownloadTask
500  {
501  public:
503 
507  virtual ~DownloadTask();
508 
513  int64 getTotalLength() const { return contentLength; }
514 
516  int64 getLengthDownloaded() const { return downloaded; }
517 
519  bool isFinished() const { return finished; }
520 
527  int statusCode() const { return httpCode; }
528 
530  inline bool hadError() const { return error; }
531 
533  File getTargetLocation() const { return targetLocation; }
534 
535  protected:
536  int64 contentLength = -1, downloaded = 0;
537  bool finished = false, error = false;
538  int httpCode = -1;
539  File targetLocation;
540 
541  DownloadTask();
542 
543  private:
544  friend class URL;
545  static std::unique_ptr<DownloadTask> createFallbackDownloader (const URL&, const File&, const DownloadTaskOptions&);
546 
547  public:
548  #if JUCE_IOS
550  static void juce_iosURLSessionNotify (const String&);
551  #endif
552 
553  private:
554  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DownloadTask)
555  };
556 
558  [[deprecated ("Use the overload with a DownloadTaskOptions argument instead")]]
559  std::unique_ptr<DownloadTask> downloadToFile (const File& targetLocation,
560  String extraHeaders = String(),
561  DownloadTaskListener* listener = nullptr,
562  bool usePostCommand = false);
563 
573  std::unique_ptr<DownloadTask> downloadToFile (const File& targetLocation,
574  const DownloadTaskOptions& options);
575 
576  //==============================================================================
591  bool readEntireBinaryStream (MemoryBlock& destData,
592  bool usePostCommand = false) const;
593 
609  String readEntireTextStream (bool usePostCommand = false) const;
610 
624  std::unique_ptr<XmlElement> readEntireXmlStream (bool usePostCommand = false) const;
625 
626  //==============================================================================
645  static String addEscapeChars (const String& stringToAddEscapeCharsTo,
646  bool isParameter,
647  bool roundBracketsAreLegal = true);
648 
658  static String removeEscapeChars (const String& stringToRemoveEscapeCharsFrom);
659 
665  static URL createWithoutParsing (const String& url);
666 
667  //==============================================================================
668  #ifndef DOXYGEN
669  using OpenStreamProgressCallback = bool (void* context, int bytesSent, int totalBytes);
670 
675  [[deprecated ("New code should use the method which takes an InputStreamOptions argument instead.")]]
676  std::unique_ptr<InputStream> createInputStream (bool doPostLikeRequest,
677  OpenStreamProgressCallback* progressCallback = nullptr,
678  void* progressCallbackContext = nullptr,
679  String extraHeaders = {},
680  int connectionTimeOutMs = 0,
681  StringPairArray* responseHeaders = nullptr,
682  int* statusCode = nullptr,
683  int numRedirectsToFollow = 5,
684  String httpRequestCmd = {}) const;
685  #endif
686 
687 private:
688  //==============================================================================
689  #if JUCE_IOS
690  struct Bookmark : public ReferenceCountedObject
691  {
692  using Ptr = ReferenceCountedObjectPtr<Bookmark>;
693 
694  Bookmark (void*);
695  ~Bookmark();
696 
697  void* data;
698  };
699 
700  Bookmark::Ptr bookmark;
701 
702  friend void setURLBookmark (URL&, void*);
703  friend void* getURLBookmark (URL&);
704  #endif
705 
706  //==============================================================================
707  struct Upload : public ReferenceCountedObject
708  {
709  Upload (const String&, const String&, const String&, const File&, MemoryBlock*);
710  String parameterName, filename, mimeType;
711  File file;
712  std::unique_ptr<MemoryBlock> data;
713 
714  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Upload)
715  };
716 
717  //==============================================================================
718  friend class WebInputStream;
719 
720  URL (const String&, int);
721  void init();
722  void addParameter (const String&, const String&);
723  bool hasBodyDataToSend() const;
724  void createHeadersAndPostData (String&, MemoryBlock&, bool) const;
725  URL withUpload (Upload*) const;
726 
727  static ParameterHandling toHandling (bool);
728  static File fileFromFileSchemeURL (const URL&);
729  String getDomainInternal (bool) const;
730 
731  //==============================================================================
732  String url;
733  MemoryBlock postData;
734  StringArray parameterNames, parameterValues;
735  String anchor;
736 
737  ReferenceCountedArray<Upload> filesToUpload;
738 
739  //==============================================================================
740  JUCE_LEAK_DETECTOR (URL)
741 };
742 
743 } // namespace juce
auto withSharedContainer(String value) const
Definition: juce_URL.h:477
auto withExtraHeaders(String value) const
Definition: juce_URL.h:468
auto withListener(DownloadTaskListener *value) const
Definition: juce_URL.h:480
auto withUsePost(bool value) const
Definition: juce_URL.h:483
bool hadError() const
Definition: juce_URL.h:530
int64 getLengthDownloaded() const
Definition: juce_URL.h:516
bool isFinished() const
Definition: juce_URL.h:519
File getTargetLocation() const
Definition: juce_URL.h:533
int statusCode() const
Definition: juce_URL.h:527
int64 getTotalLength() const
Definition: juce_URL.h:513
const StringArray & getParameterNames() const noexcept
Definition: juce_URL.h:249
const StringArray & getParameterValues() const noexcept
Definition: juce_URL.h:263
const MemoryBlock & getPostDataAsMemoryBlock() const noexcept
Definition: juce_URL.h:289
String getPostData() const
Definition: juce_URL.h:286
std::unique_ptr< DownloadTask > downloadToFile(const File &targetLocation, const DownloadTaskOptions &options)
virtual void finished(DownloadTask *task, bool success)=0