libUPnP  1.6.26
httpreadwrite.h
1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  * Copyright (c) 2012 France Telecom All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * - Neither name of Intel Corporation nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  ******************************************************************************/
32 
33 #ifndef GENLIB_NET_HTTP_HTTPREADWRITE_H
34 #define GENLIB_NET_HTTP_HTTPREADWRITE_H
35 
36 /*
37  * \file
38  */
39 
40 #include "config.h"
41 #include "upnputil.h"
42 #include "sock.h"
43 #include "httpparser.h"
44 
46 #define HTTP_DEFAULT_TIMEOUT 30
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #ifdef WIN32
53 struct tm *http_gmtime_r(const time_t *clock, struct tm *result);
54 #else
55 #define http_gmtime_r gmtime_r
56 #endif
57 
58 int http_CancelHttpGet(IN void *Handle);
59 
67 int http_FixUrl(
69  uri_type *url,
71  uri_type *fixed_url);
72 
80 int http_FixStrUrl(
82  const char *urlstr,
84  size_t urlstrlen,
86  uri_type *fixed_url);
87 
96 SOCKET http_Connect(
98  uri_type *destination_url,
100  uri_type *url);
101 
102 
103 /************************************************************************
104  * Function: http_RecvMessage
105  *
106  * Parameters:
107  * IN SOCKINFO *info; Socket information object
108  * OUT http_parser_t* parser; HTTP parser object
109  * IN http_method_t request_method; HTTP request method
110  * IN OUT int* timeout_secs; time out
111  * OUT int* http_error_code; HTTP error code returned
112  *
113  * Description:
114  * Get the data on the socket and take actions based on the read data
115  * to modify the parser objects buffer. If an error is reported while
116  * parsing the data, the error code is passed in the http_errr_code
117  * parameter
118  *
119  * Returns:
120  * UPNP_E_BAD_HTTPMSG
121  * UPNP_E_SUCCESS
122  ************************************************************************/
123 int http_RecvMessage( IN SOCKINFO *info, OUT http_parser_t* parser,
124  IN http_method_t request_method,
125  IN OUT int* timeout_secs,
126  OUT int* http_error_code );
127 
128 
151 int http_SendMessage(
152  /* [in] Socket information object. */
153  SOCKINFO *info,
154  /* [in,out] Time out value. */
155  int* timeout_secs,
156  /* [in] Pattern format to take actions upon. */
157  const char* fmt,
158  /* [in] Variable parameter list. */
159  ...);
160 
161 /************************************************************************
162  * Function: http_RequestAndResponse
163  *
164  * Parameters:
165  * IN uri_type* destination; Destination URI object which contains
166  * remote IP address among other elements
167  * IN const char* request; Request to be sent
168  * IN size_t request_length; Length of the request
169  * IN http_method_t req_method; HTTP Request method
170  * IN int timeout_secs; time out value
171  * OUT http_parser_t* response; Parser object to receive the repsonse
172  *
173  * Description:
174  * Initiates socket, connects to the destination, sends a
175  * request and waits for the response from the remote end
176  *
177  * Returns:
178  * UPNP_E_SOCKET_ERROR
179  * UPNP_E_SOCKET_CONNECT
180  * Error Codes returned by http_SendMessage
181  * Error Codes returned by http_RecvMessage
182  ************************************************************************/
183 int http_RequestAndResponse(
184  IN uri_type* destination,
185  IN const char* request,
186  IN size_t request_length,
187  IN http_method_t req_method,
188  IN int timeout_secs,
189  OUT http_parser_t* response );
190 
191 
192 /************************************************************************
193  * return codes:
194  * 0 -- success
195  * UPNP_E_OUTOF_MEMORY
196  * UPNP_E_TIMEDOUT
197  * UPNP_E_BAD_REQUEST
198  * UPNP_E_BAD_RESPONSE
199  * UPNP_E_INVALID_URL
200  * UPNP_E_SOCKET_READ
201  * UPNP_E_SOCKET_WRITE
202  ************************************************************************/
203 
204 
205 /************************************************************************
206  * Function: http_Download
207  *
208  * Parameters:
209  * IN const char* url_str; String as a URL
210  * IN int timeout_secs; time out value
211  * OUT char** document; buffer to store the document extracted
212  * from the donloaded message.
213  * OUT size_t* doc_length; length of the extracted document
214  * OUT char* content_type; Type of content
215  *
216  * Description:
217  * Download the document message and extract the document
218  * from the message.
219  *
220  * Return: int
221  * UPNP_E_SUCCESS
222  * UPNP_E_INVALID_URL
223  ************************************************************************/
224 int http_Download(
225  IN const char* url,
226  IN int timeout_secs,
227  OUT char** document,
228  OUT size_t *doc_length,
229  OUT char* content_type );
230 
231 
232 /************************************************************************
233  * Function: http_WriteHttpPost
234  *
235  * Parameters:
236  * IN void *Handle: Handle to the http post object
237  * IN char *buf: Buffer to send to peer, if format used
238  * is not UPNP_USING_CHUNKED,
239  * IN size_t *size: Size of the data to be sent.
240  * IN int timeout: time out value
241  *
242  * Description:
243  * Formats data if format used is UPNP_USING_CHUNKED.
244  * Writes data on the socket connected to the peer.
245  *
246  * Return: int
247  * UPNP_E_SUCCESS - On Success
248  * UPNP_E_INVALID_PARAM - Invalid Parameter
249  * -1 - On Socket Error.
250  ************************************************************************/
251 int http_WriteHttpPost(IN void *Handle,
252  IN char *buf,
253  IN size_t *size,
254  IN int timeout);
255 
256 
257 /************************************************************************
258  * Function: http_CloseHttpPost
259  *
260  * Parameters:
261  * IN void *Handle; Handle to the http post object
262  * IN OUT int *httpStatus; HTTP status returned on receiving a
263  * response message
264  * IN int timeout; time out value
265  *
266  * Description:
267  * Sends remaining data if using UPNP_USING_CHUNKED
268  * format. Receives any more messages. Destroys socket and any socket
269  * associated memory. Frees handle associated with the HTTP POST msg.
270  *
271  * Return: int
272  * UPNP_E_SUCCESS - On success
273  * UPNP_E_INVALID_PARAM - Invalid Parameter
274  ************************************************************************/
275 int http_CloseHttpPost(IN void *Handle,
276  IN OUT int *httpStatus,
277  IN int timeout);
278 
279 
280 /************************************************************************
281  * Function: http_OpenHttpPost
282  *
283  * Parameters:
284  * IN const char *url_str; String as a URL
285  * IN OUT void **Handle; Pointer to buffer to store HTTP
286  * post handle
287  * IN const char *contentType; Type of content
288  * IN int contentLength; length of content
289  * IN int timeout; time out value
290  *
291  * Description:
292  * Makes the HTTP POST message, connects to the peer,
293  * sends the HTTP POST request. Adds the post handle to buffer of
294  * such handles
295  *
296  * Return : int;
297  * UPNP_E_SUCCESS - On success
298  * UPNP_E_INVALID_PARAM - Invalid Parameter
299  * UPNP_E_OUTOF_MEMORY
300  * UPNP_E_SOCKET_ERROR
301  * UPNP_E_SOCKET_CONNECT
302  ************************************************************************/
303 int http_OpenHttpPost(IN const char *url_str,
304  IN OUT void **Handle,
305  IN const char *contentType,
306  IN int contentLength,
307  IN int timeout);
308 
309 
310 /************************************************************************
311  * Function: http_ReadHttpGet
312  *
313  * Parameters:
314  * IN void *Handle; Handle to the HTTP get object
315  * IN OUT char *buf; Buffer to get the read and parsed data
316  * IN OUT size_t *size; Size of the buffer passed
317  * IN int timeout; time out value
318  *
319  * Description:
320  * Parses already existing data, then gets new data.
321  * Parses and extracts information from the new data.
322  *
323  * Return: int
324  * UPNP_E_SUCCESS - On success
325  * UPNP_E_INVALID_PARAM - Invalid Parameter
326  * UPNP_E_BAD_RESPONSE
327  * UPNP_E_BAD_HTTPMSG
328  * UPNP_E_CANCELED
329  ************************************************************************/
330 int http_ReadHttpGet(
331  IN void *Handle,
332  IN OUT char *buf,
333  IN OUT size_t *size,
334  IN int timeout);
335 
336 
337 /************************************************************************
338  * Function: http_HttpGetProgress
339  *
340  * Parameters:
341  * IN void *Handle; Handle to the HTTP get object
342  * OUT size_t *length; Buffer to get the read and parsed data
343  * OUT size_t *total; Size of tge buffer passed
344  *
345  * Description:
346  * Extracts information from the Handle to the HTTP get object.
347  *
348  * Return: int
349  * UPNP_E_SUCCESS - On Sucess
350  * UPNP_E_INVALID_PARAM - Invalid Parameter
351  ************************************************************************/
352 int http_HttpGetProgress(
353  IN void *Handle,
354  OUT size_t *length,
355  OUT size_t *total);
356 
357 /************************************************************************
358  * Function: http_CloseHttpGet
359  *
360  * Parameters:
361  * IN void *Handle; Handle to HTTP get object
362  *
363  * Description:
364  * Clears the handle allocated for the HTTP GET operation
365  * Clears socket states and memory allocated for socket operations.
366  *
367  * Return: int
368  * UPNP_E_SUCCESS - On Success
369  * UPNP_E_INVALID_PARAM - Invalid Parameter
370  ************************************************************************/
371 int http_CloseHttpGet(IN void *Handle);
372 
386 int http_OpenHttpGet(
387  /* [in] String as a URL. */
388  const char *url_str,
389  /* [in,out] Pointer to buffer to store HTTP post handle. */
390  void **Handle,
391  /* [in,out] Type of content. */
392  char **contentType,
393  /* [out] length of content. */
394  int *contentLength,
395  /* [out] HTTP status returned on receiving a response message. */
396  int *httpStatus,
397  /* [in] time out value. */
398  int timeout);
399 
414  /* [in] String as a URL. */
415  const char *url_str,
416  /* [in] String as a URL. */
417  const char *proxy_str,
418  /* [in,out] Pointer to buffer to store HTTP post handle. */
419  void **Handle,
420  /* [in,out] Type of content. */
421  char **contentType,
422  /* [out] length of content. */
423  int *contentLength,
424  /* [out] HTTP status returned on receiving a response message. */
425  int *httpStatus,
426  /* [in] time out value. */
427  int timeout);
428 
429 
430 /************************************************************************
431  * Function: http_SendStatusResponse
432  *
433  * Parameters:
434  * IN SOCKINFO *info; Socket information object
435  * IN int http_status_code; error code returned while making
436  * or sending the response message
437  * IN int request_major_version; request major version
438  * IN int request_minor_version; request minor version
439  *
440  * Description:
441  * Generate a response message for the status query and send the
442  * status response.
443  *
444  * Return: int
445  * 0 -- success
446  * UPNP_E_OUTOF_MEMORY
447  * UPNP_E_SOCKET_WRITE
448  * UPNP_E_TIMEDOUT
449  ************************************************************************/
450 int http_SendStatusResponse(
451  IN SOCKINFO *info,
452  IN int http_status_code,
453  IN int request_major_version,
454  IN int request_minor_version );
455 
494 int http_MakeMessage(
495  /* [in,out] Buffer with the contents of the message. */
496  INOUT membuffer* buf,
497  /* [in] HTTP major version. */
498  IN int http_major_version,
499  /* [in] HTTP minor version. */
500  IN int http_minor_version,
501  /* [in] Pattern format. */
502  IN const char* fmt,
503  /* [in] Format arguments. */
504  ... );
505 
506 
507 /************************************************************************
508  * Function: http_CalcResponseVersion
509  *
510  * Parameters:
511  * IN int request_major_vers; Request major version
512  * IN int request_minor_vers; Request minor version
513  * OUT int* response_major_vers; Response mojor version
514  * OUT int* response_minor_vers; Response minor version
515  *
516  * Description:
517  * Calculate HTTP response versions based on the request versions.
518  *
519  * Return: void
520  ************************************************************************/
521 void http_CalcResponseVersion(
522  IN int request_major_vers,
523  IN int request_minor_vers,
524  OUT int* response_major_vers,
525  OUT int* response_minor_vers );
526 
527 
528 /************************************************************************
529  * Function: http_OpenHttpGetEx
530  *
531  * Parameters:
532  * IN const char *url_str; String as a URL
533  * IN OUT void **Handle; Pointer to buffer to store HTTP
534  * post handle
535  * IN OUT char **contentType; Type of content
536  * OUT int *contentLength; length of content
537  * OUT int *httpStatus; HTTP status returned on receiving a
538  * response message
539  * IN int timeout; time out value
540  *
541  * Description:
542  * Makes the HTTP GET message, connects to the peer,
543  * sends the HTTP GET request, gets the response and parses the
544  * response.
545  *
546  * Return: int
547  * UPNP_E_SUCCESS - On Success
548  * UPNP_E_INVALID_PARAM - Invalid Paramters
549  * UPNP_E_OUTOF_MEMORY
550  * UPNP_E_SOCKET_ERROR
551  * UPNP_E_BAD_RESPONSE
552  ************************************************************************/
553 int http_OpenHttpGetEx(IN const char *url_str,
554  IN OUT void **Handle,
555  IN OUT char **contentType,
556  OUT int *contentLength,
557  OUT int *httpStatus,
558  IN int lowRange,
559  IN int highRange,
560  IN int timeout);
561 
562 
563 /************************************************************************
564  * Function: get_sdk_info
565  *
566  * Parameters:
567  * OUT char *info; buffer to store the operating system information
568  * IN size_t infoSize; size of buffer
569  *
570  * Description:
571  * Returns the server information for the operating system
572  *
573  * Return:
574  * UPNP_INLINE void
575  ************************************************************************/
576 void get_sdk_info( OUT char *info, IN size_t infoSize );
577 
578 #ifdef __cplusplus
579 } /* #extern "C" */
580 #endif
581 
582 
583 #endif /* GENLIB_NET_HTTP_HTTPREADWRITE_H */
584 
int http_RecvMessage(IN SOCKINFO *info, OUT http_parser_t *parser, IN http_method_t request_method, IN OUT int *timeout_secs, OUT int *http_error_code)
Get the data on the socket and take actions based on the read data to modify the parser objects buffe...
Definition: httpreadwrite.c:284
int http_OpenHttpGetProxy(const char *url_str, const char *proxy_str, void **Handle, char **contentType, int *contentLength, int *httpStatus, int timeout)
Makes the HTTP GET message, connects to the peer, sends the HTTP GET request, gets the response and p...
Definition: httpreadwrite.c:1475
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:136
Definition: sock.h:55
int http_MakeMessage(membuffer *buf, int http_major_version, int http_minor_version, const char *fmt,...)
Generate an HTTP message based on the format that is specified in the input parameters.
Definition: httpreadwrite.c:1625
int SOCKET
Definition: UpnpInet.h:53
int http_SendMessage(SOCKINFO *info, int *TimeOut, const char *fmt,...)
Sends a message to the destination based on the format parameter.
Definition: httpreadwrite.c:375
Definition: httpparser.h:209
Definition: membuffer.h:56