File: juce_WebInputStream.h

package info (click to toggle)
libopenshot-audio 0.5.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,188 kB
  • sloc: cpp: 132,582; java: 1,318; makefile: 18; sh: 15
file content (233 lines) | stat: -rw-r--r-- 9,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
  ==============================================================================

   This file is part of the JUCE library.
   Copyright (c) 2022 - Raw Material Software Limited

   JUCE is an open source library subject to commercial or open-source
   licensing.

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   To use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
   EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
   DISCLAIMED.

  ==============================================================================
*/

namespace juce
{

//==============================================================================
/**
    An InputStream which can be used to read from a given URL.

    @tags{Core}
*/
class JUCE_API WebInputStream  : public InputStream
{
 public:
    /** Creates a new WebInputStream which can be used to read from a URL.

        @param url      the URL that should be retrieved. This parameter may also contain
                        POST data and/or parameters.
        @param addParametersToRequestBody  specifies whether any URL parameters that have
                        been set will be transferred via the request body data or added
                        to the URL address. This will also determine whether a POST or GET
                        command will be used if a custom command is not set.
    */
    WebInputStream (const URL& url, bool addParametersToRequestBody);

    /** Destructor. */
    ~WebInputStream() override;

    /** Add extra headers to the HTTP request.

        Returns a reference to itself so that several methods can be chained.

        @param extraHeaders   this string is appended onto the headers that are used for
                              the request. It must therefore be a valid set of HTML
                              header directives, separated by newlines.
    */
    WebInputStream& withExtraHeaders (const String& extraHeaders);

    /** Override the HTTP command that is sent.

        Returns a reference to itself so that several methods can be chained.

        Note that this command will not change the way parameters are sent. This
        must be specified in the constructor.

        @param customRequestCommand  this string is the custom HTTP request command such
                                     as POST or GET.
    */
    WebInputStream& withCustomRequestCommand (const String& customRequestCommand);

    /** Specify the connection time-out.

        Returns a reference to itself so that several methods can be chained.

        @param timeoutInMs  the number of milliseconds to wait until the connection
                            request is aborted.
    */
    WebInputStream& withConnectionTimeout (int timeoutInMs);

    /** Specify the number of redirects to be followed.

        Returns a reference to itself so that several methods can be chained.

        @param numRedirects  specifies the number of redirects that will be followed
                             before returning a response (ignored for Android which
                             follows up to 5 redirects)
    */
    WebInputStream& withNumRedirectsToFollow (int numRedirects);

    //==============================================================================
    /** Used to receive callbacks for POST data send progress.

        Pass one of these into the connect() method and its postDataSendProgress()
        method will be called periodically with updates on POST data upload progress.
    */
    class JUCE_API Listener
    {
    public:
        /** Destructor. */
        virtual ~Listener() = default;

        /** This method will be called periodically with updates on POST data upload progress.

            @param request     the original request
            @param bytesSent   the number of bytes sent so far
            @param totalBytes  the total number of bytes to send

            @returns true to continue or false to cancel the upload
        */
        virtual bool postDataSendProgress (WebInputStream& request, int bytesSent, int totalBytes);
    };

    /** Wait until the first byte is ready for reading.

        This method will attempt to connect to the URL given in the constructor
        and block until the status code and all response headers have been received or
        an error has occurred.

        Note that most methods will call connect() internally if they are called without
        an established connection. Therefore, it is not necessary to explicitly
        call connect unless you would like to use a custom listener.

        After a successful call to connect(), getResponseHeaders(), getTotalLength()
        and getStatusCode() will all be non-blocking.

        @param listener    a listener to receive progress callbacks on the status
                           of a POST data upload.

        @see getResponseHeaders, getTotalLength, getStatusCode
    */
    bool connect (Listener* listener);

    /** Returns true if there was an error during the connection attempt. */
    bool isError() const;

    /** Will cancel a blocking read and prevent any subsequent connection attempts. */
    void cancel();

    /** Returns a StringPairArray of the request headers. */
    StringPairArray getRequestHeaders() const;

    /** Returns a StringPairArray of response headers.

        If getResponseHeaders() is called without an established connection, then
        getResponseHeaders() will call connect internally and block until connect
        returns - either due to a successful connection or a connection
        error.

        @see connect
    */
    StringPairArray getResponseHeaders();

    /** Returns the status code returned by the HTTP server

        If getStatusCode() is called without an established connection, then
        getStatusCode() will call connect internally and block until connect
        returns - either due to a successful connection or a connection
        error.

        @see connect
    */
    int getStatusCode();

    //==============================================================================
    /** Returns the total number of bytes available for reading in this stream.

        Note that this is the number of bytes available from the start of the
        stream, not from the current position.

        If getTotalLength() is called without an established connection, then
        getTotalLength() will call connect internally and block until connect
        returns - either due to a successful connection or a connection
        error.

        If the size of the stream isn't actually known, this will return -1.
    */
    int64 getTotalLength() override;

    /** Reads some data from the stream into a memory buffer.

        This method will block until the maxBytesToRead bytes are available.

        This method calls connect() internally if the connection hasn't already
        been established.

        @param destBuffer       the destination buffer for the data. This must not be null.
        @param maxBytesToRead   the maximum number of bytes to read - make sure the
                                memory block passed in is big enough to contain this
                                many bytes. This value must not be negative.

        @returns    the actual number of bytes that were read, which may be less than
                    maxBytesToRead if the stream is exhausted before it gets that far
    */
    int read (void* destBuffer, int maxBytesToRead) override;

    /** Returns true if the stream has no more data to read. */
    bool isExhausted() override;

    /** Returns the offset of the next byte that will be read from the stream.

        @see setPosition
    */
    int64 getPosition() override;

    /** Tries to move the current read position of the stream.

        The position is an absolute number of bytes from the stream's start.

        For a WebInputStream, this method will fail if wantedPos is smaller
        than the current position. If wantedPos is greater than the current
        position, then calling setPosition() is the same as calling read(), i.e.
        the skipped data will still be downloaded, although skipped bytes will
        be discarded immediately.

        @returns  true if the stream manages to reposition itself correctly
        @see getPosition
    */
    bool setPosition (int64 wantedPos) override;

 private:
    static void createHeadersAndPostData (const URL&, String&, MemoryBlock&, bool);
    static StringPairArray parseHttpHeaders (const String&);

    class Pimpl;
    friend class Pimpl;

    std::unique_ptr<Pimpl> pimpl;
    bool hasCalledConnect = false;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebInputStream)
};

} // namespace juce