libUPnP 1.8.0
|
00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2000-2003 Intel Corporation 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright notice, 00010 * this list of conditions and the following disclaimer. 00011 * - Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * - Neither name of Intel Corporation nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 ******************************************************************************/ 00031 00032 00033 #ifndef GENLIB_NET_URI_H 00034 #define GENLIB_NET_URI_H 00035 00036 00041 #if !defined(WIN32) 00042 #include <sys/param.h> 00043 #endif 00044 00045 00046 #include "UpnpGlobal.h" /* for */ 00047 #include "UpnpInet.h" 00048 00049 00050 #include <ctype.h> 00051 #include <errno.h> 00052 #include <fcntl.h> 00053 #include <stdlib.h> 00054 #include <string.h> 00055 #if defined(BSD) 00056 #include <sys/socket.h> 00057 #endif 00058 #include <sys/types.h> 00059 #include <time.h> 00060 00061 00062 #ifdef WIN32 00063 #else 00064 #include <arpa/inet.h> /* for inet_pton() */ 00065 #include <netdb.h> /* for struct addrinfo */ 00066 #endif 00067 00068 00069 #ifdef WIN32 00070 #define strncasecmp strnicmp 00071 #else 00072 /* Other systems have strncasecmp */ 00073 #endif 00074 00075 00076 #ifdef __cplusplus 00077 extern "C" { 00078 #endif 00079 00080 00081 #define HTTP_DATE_LENGTH 37 // length for HTTP DATE: 00082 //"DATE: Sun, 01 Jul 2000 08:15:23 GMT<cr><lf>" 00083 #define SEPARATORS "()<>@,;:\\\"/[]?={} \t" 00084 #define MARK "-_.!~*'()" 00085 #define RESERVED ";/?:@&=+$,{}" //added {} for compatibility 00086 #define HTTP_SUCCESS 1 00087 00088 00089 #define FALSE 0 00090 #define TAB 9 00091 #define CR 13 00092 #define LF 10 00093 #define SOCKET_BUFFER_SIZE 5000 00094 00095 00096 enum hostType { 00097 HOSTNAME, 00098 IPv4address 00099 }; 00100 00101 00102 enum pathType { 00103 ABS_PATH, 00104 REL_PATH, 00105 OPAQUE_PART 00106 }; 00107 00108 00109 #ifdef WIN32 00110 /* there is a conflict in windows with other symbols */ 00111 enum uriType { 00112 absolute, 00113 relative 00114 }; 00115 #else 00116 enum uriType { 00117 ABSOLUTE, 00118 RELATIVE 00119 }; 00120 #endif 00121 00122 00127 typedef struct TOKEN { 00128 const char *buff; 00129 size_t size; 00130 } token; 00131 00132 00137 typedef struct HOSTPORT { 00139 token text; 00140 /* Network Byte Order */ 00141 struct sockaddr_storage IPaddress; 00142 } hostport_type; 00143 00144 00148 typedef struct URI{ 00149 enum uriType type; 00150 token scheme; 00151 enum pathType path_type; 00152 token pathquery; 00153 token fragment; 00154 hostport_type hostport; 00155 } uri_type; 00156 00157 00162 typedef struct URL_LIST { 00164 int size; 00166 char *URLs; 00168 uri_type *parsedURLs; 00169 } URL_list; 00170 00171 00184 int replace_escaped( 00186 char *in, 00188 int index, 00190 size_t *max); 00191 00192 00204 int copy_URL_list( 00206 URL_list *in, 00208 URL_list *out); 00209 00216 void free_URL_list( 00218 URL_list *list); 00219 00220 00224 #ifdef DEBUG 00225 void print_uri( 00227 uri_type *in); 00228 #else 00229 static UPNP_INLINE void print_uri(uri_type *in) {} 00230 #endif 00231 00232 00236 #ifdef DEBUG 00237 void print_token( 00239 token *in); 00240 #else 00241 static UPNP_INLINE void print_token(token * in) {} 00242 #endif 00243 00244 00253 int token_string_casecmp( 00255 token *in1, 00257 char *in2); 00258 00259 00268 int token_string_cmp( 00270 token *in1, 00272 char *in2); 00273 00274 00283 int token_cmp( 00285 token *in1, 00287 token *in2); 00288 00289 00297 int parse_hostport( 00299 const char *in, 00301 int max, 00304 hostport_type *out); 00305 00306 00316 int remove_escaped_chars( 00318 char *in, 00320 size_t *size); 00321 00322 00345 int remove_dots( 00347 char *in, 00349 size_t size); 00350 00351 00366 char *resolve_rel_url( 00368 char *base_url, 00370 char *rel_url); 00371 00372 00385 int parse_uri( 00387 const char *in, 00389 int max, 00391 uri_type *out); 00392 00393 00402 int parse_uri_and_unescape( 00404 char *in, 00406 int max, 00408 uri_type *out); 00409 00410 00416 int parse_token( 00418 char *in, 00420 token *out, 00422 int max_size); 00423 00424 00425 /* Commented #defines, functions and typdefs */ 00426 00427 #if 0 00428 #define HTTP_E_BAD_URL UPNP_E_INVALID_URL 00429 #define HTTP_E_READ_SOCKET UPNP_E_SOCKET_READ 00430 #define HTTP_E_BIND_SOCKET UPNP_E_SOCKET_BIND 00431 #define HTTP_E_WRITE_SOCKET UPNP_E_SOCKET_WRITE 00432 #define HTTP_E_CONNECT_SOCKET UPNP_E_SOCKET_CONNECT 00433 #define HTTP_E_SOCKET UPNP_E_OUTOF_SOCKET 00434 #define HTTP_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE 00435 #define HTTP_E_BAD_REQUEST UPNP_E_BAD_REQUEST 00436 #define HTTP_E_BAD_IP_ADDRESS UPNP_E_INVALID_URL 00437 00438 #define RESPONSE_TIMEOUT 30 00439 #endif 00440 00441 #if 0 00442 00446 typedef struct SOCKET_BUFFER{ 00447 char buff[SOCKET_BUFFER_SIZE]; 00448 int size; 00449 struct SOCKET_BUFFER *next; 00450 } socket_buffer; 00451 00452 00453 typedef struct HTTP_HEADER { 00454 token header; 00455 token value; 00456 struct HTTP_HEADER * next; 00457 } http_header; 00458 00459 00460 typedef struct HTTP_STATUS_LINE{ 00461 token http_version; 00462 token status_code; 00463 token reason_phrase; 00464 } http_status; 00465 00466 00467 typedef struct HTTP_REQUEST_LINE { 00468 token http_version; 00469 uri_type request_uri; 00470 token method; 00471 } http_request; 00472 00473 00477 typedef struct HTTP_MESSAGE { 00478 http_status status; 00479 http_request request; 00480 http_header * header_list; 00481 token content; 00482 } http_message; 00483 #endif 00484 00485 00486 #if 0 00487 int transferHTTP( 00488 char *request, 00489 char *toSend, 00490 int toSendSize, 00491 char **out, 00492 char *Url); 00493 00494 00495 int transferHTTPRaw( 00496 char *toSend, 00497 int toSendSize, 00498 char **out, 00499 char *URL); 00500 00501 00505 int transferHTTPparsedURL( 00506 char *request, 00507 char *toSend, 00508 int toSendSize, 00509 char **out, 00510 uri_type *URL); 00511 00512 00518 void currentTmToHttpDate( 00519 char *out); 00520 00521 00522 int parse_http_response( 00523 char *in, 00524 http_message *out, 00525 int max_len); 00526 00527 00528 int parse_http_request( 00529 char *in, 00530 http_message *out, 00531 int max_len); 00532 00533 00534 void print_http_message( 00535 http_message *message); 00536 00537 00538 int search_for_header( 00539 http_message *in, 00540 char *header, 00541 token *out_value); 00542 00543 00544 void print_status_line( 00545 http_status *in); 00546 00547 00548 void print_request_line( 00549 http_request *in); 00550 00551 00552 int parse_http_line( 00553 char *in, 00554 int max_size); 00555 00556 00557 int parse_not_LWS( 00558 char *in, 00559 token *out, 00560 int max_size); 00561 00562 00563 int parse_LWS( 00564 char *in, 00565 int max_size); 00566 00567 00568 size_t write_bytes( 00569 int fd, 00570 char *bytes, 00571 size_t n, 00572 int timeout); 00573 00574 00575 void free_http_message( 00576 http_message *message); 00577 00578 00579 #endif 00580 00581 00582 #ifdef __cplusplus 00583 } 00584 #endif 00585 00586 00587 #endif /* GENLIB_NET_URI_H */ 00588