summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/http_client/http_client_abstract.h
blob: 5e9d1d5e62adaa94a3cc6858cd2335d7cb78a161 (plain)
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
#undef DLIB_BROWSER_ABSTRACh_
#ifdef DLIB_BROWSER_ABSTRACh_



namespace dlib
{

    // Function which is called when there is data available.
    //   Return false to stop the download process...
    typedef bool (*fnOnDownload)(long already_downloaded, long total_to_download, void * userInfo);


// ----------------------------------------------------------------------------------------
/*
TODO:
- Timed cookie support
- POSTing files: check it!
- Don't timeout when still downloading!
*/
// ----------------------------------------------------------------------------------------


    class Browser
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a possibility for the end user to download webpages (HTTP/1.0)
                from the internet like a normal webbrowser would do.
        !*/

    public:

        Browser(
        );
        /*!
            Constructor
        !*/

        void set_header(
            const std::string& header_name,
            const std::string& header_value
        );
        /*!
            Set a header to a certain value
            Example: set_header("User-Agent", "Internet Explorer")
        !*/


        void set_header(
            const std::string& header_name,
            long header_value
        );
        /*!
            Set a header to a certain number
            Example: set_header("Content-Length", 1234)
        !*/

        std::string get_header(
            const std::string& header_name
        ) const;
        /*!
            Get the value of the header or an empty string when it's not set.
            Example: get_header("Content-Length") would return "1234"
        !*/

        void remove_header(
            const std::string& header_name
        );
        /*!
            Removes a certain header
        !*/

        bool is_header_set(
            const std::string& header_name
        ) const;
        /*!
            Returns when a header is set and is not empty
        !*/

        void set_user_agent(
            const std::string& new_agent
        ) { set_header("User-Agent", new_agent); }
        /*!
            Convenience function for setting a user agent
        !*/

        void clear(
        );
        /*!
            Clear out all cookies & headers set until now
        !*/

        void prepare_for_next_url(
        );
        /*!
            Clear out any header and/or cookie which would obstruct getting a next page.
            At this moment this is cleared:
                - the Content-Type header
        !*/

        void set_callback_function( 
            fnOnDownload od, 
            void * _user_info 
        );
        /*!
            Set a callback function for one of the following events:
            - OnDownload: this will tell you how much is downloaded and how much will need to be downloaded
        !*/

        void set_cookie(
            const std::string& cookie_name, 
            const std::string& cookie_value
        );
        /*!
            Set a cookie
        !*/

        void set_cookie(
            const std::string& cookie_name, 
            long cookie_value
        );
        /*!
            Set a cookie
        !*/

        void remove_cookie(
            const std::string& cookie_name
        );
        /*!
            Remove a cookie if it's set
        !*/

        void set_timeout( 
            unsigned int milliseconds  
        );
        /*!
            Set the maximum time how long a request can take. Setting this to 0 disables
            this behavior.
        !*/

        string_to_stringvector get_returned_headers(
        ) const; 
        /*!
            Returns all the headers which are returned in the download of the webpage.
        !*/

        short get_http_return (
        ) const;
        /*!
            Retrieves the HTTP return code.
        !*/

        const std::string& get_body (
        ) const; 
        /*!
            Retrieves the HTTP body.
        !*/

        const std::string& post_url (
            const std::string& url, 
            const string_to_stringmap& postvars,
            const string_to_stringmap& filenames = string_to_stringmap()
        );
        /*!
            POST an url to the internet.
            You can pass the post variables as well as a list of filenames
        !*/

        const std::string& post_url (
            const std::string& url, 
            const std::string& postbuffer
        );
        /*!
            POST an url to the internet.
            In this function you have constructed the POST string yourselves
        !*/

        const std::string& get_url (
            const std::string& url
        );
        /*!
            GET an url from the internet.
        !*/

        bool has_error( 
        ) const;
        /*!
            Has there happened an error?
        !*/

        const std::string& get_error( 
        ) const;
        /*!
            Get the error explanation
        !*/

        static std::string urlencode(
            const std::string& in, 
            bool post_encode = false
        );
        /*!
            Convenience function to URLencode a string
        !*/

        static std::string urldecode(
            const std::string& in
        );
        /*!
            Convenience function to URLdecode a string
        !*/

    };

}

#endif // DLIB_BROWSER_ABSTRACh_