libUPnP  1.6.20
uri.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 #ifndef GENLIB_NET_URI_H
33 #define GENLIB_NET_URI_H
34 
39 #if !defined(WIN32)
40  #include <sys/param.h>
41 #endif
42 
43 #include "UpnpGlobal.h" /* for */
44 #include "UpnpInet.h"
45 
46 #include <ctype.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/types.h>
52 #include <time.h>
53 
54 #ifdef WIN32
55  #ifndef UPNP_USE_MSVCPP
56  /* VC Winsocks2 includes these functions */
57  #include "inet_pton.h"
58  #endif
59 #else
60  #include <netdb.h> /* for struct addrinfo */
61 #endif
62 
63 #ifdef WIN32
64  #define strncasecmp strnicmp
65 #else
66  /* Other systems have strncasecmp */
67 #endif
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
74 #define HTTP_DATE_LENGTH 37
75 
76 #define SEPARATORS "()<>@,;:\\\"/[]?={} \t"
77 #define MARK "-_.!~*'()"
78 
80 #define RESERVED ";/?:@&=+$,{}"
81 
82 #define HTTP_SUCCESS 1
83 #define FALSE 0
84 #define TAB 9
85 #define CR 13
86 #define LF 10
87 #define SOCKET_BUFFER_SIZE 5000
88 
89 enum hostType {
90  HOSTNAME,
91  IPv4address
92 };
93 
94 enum pathType {
95  ABS_PATH,
96  REL_PATH,
97  OPAQUE_PART
98 };
99 
100 #ifdef WIN32
101  /* there is a conflict in windows with other symbols. */
102  enum uriType {
103  absolute,
104  relative
105  };
106 #else
107  enum uriType {
108  ABSOLUTE,
109  RELATIVE
110  };
111 #endif
112 
117 typedef struct TOKEN {
118  const char *buff;
119  size_t size;
120 } token;
121 
126 typedef struct HOSTPORT {
129  /* Network Byte Order */
130  struct sockaddr_storage IPaddress;
131 } hostport_type;
132 
136 typedef struct URI{
137  enum uriType type;
138  token scheme;
139  enum pathType path_type;
140  token pathquery;
141  token fragment;
142  hostport_type hostport;
143 } uri_type;
144 
149 typedef struct URL_LIST {
151  size_t size;
153  char *URLs;
155  uri_type *parsedURLs;
156 } URL_list;
157 
170 int replace_escaped(
172  char *in,
174  size_t index,
176  size_t *max);
177 
189 int copy_URL_list(
191  URL_list *in,
193  URL_list *out);
194 
201 void free_URL_list(
203  URL_list *list);
204 
208 #ifdef DEBUG
209 void print_uri(
211  uri_type *in);
212 #else
213 static UPNP_INLINE void print_uri(uri_type *in)
214 {
215  return;
216  in = in;
217 }
218 #endif
219 
223 #ifdef DEBUG
224 void print_token(
226  token *in);
227 #else
228 static UPNP_INLINE void print_token(
230  token *in)
231 {
232  return;
233  in = in;
234 }
235 #endif
236 
247  token *in1,
249  const char *in2);
250 
259 int token_string_cmp(
261  token *in1,
263  char *in2);
264 
273 int token_cmp(
275  token *in1,
277  token *in2);
278 
290  char *in,
292  size_t *size);
293 
316 int remove_dots(
318  char *in,
320  size_t size);
321 
336 char *resolve_rel_url(
338  char *base_url,
340  char *rel_url);
341 
354 int parse_uri(
356  const char *in,
358  size_t max,
360  uri_type *out);
361 
372  char *in,
374  size_t max,
376  uri_type *out);
377 
383 int parse_token(
385  char *in,
387  token *out,
389  int max_size);
390 
391 /* Commented #defines, functions and typdefs */
392 
393 #if 0
394 #define HTTP_E_BAD_URL UPNP_E_INVALID_URL
395 #define HTTP_E_READ_SOCKET UPNP_E_SOCKET_READ
396 #define HTTP_E_BIND_SOCKET UPNP_E_SOCKET_BIND
397 #define HTTP_E_WRITE_SOCKET UPNP_E_SOCKET_WRITE
398 #define HTTP_E_CONNECT_SOCKET UPNP_E_SOCKET_CONNECT
399 #define HTTP_E_SOCKET UPNP_E_OUTOF_SOCKET
400 #define HTTP_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE
401 #define HTTP_E_BAD_REQUEST UPNP_E_BAD_REQUEST
402 #define HTTP_E_BAD_IP_ADDRESS UPNP_E_INVALID_URL
403 
404 #define RESPONSE_TIMEOUT 30
405 #endif
406 
407 #if 0
408 
412 typedef struct SOCKET_BUFFER{
413  char buff[SOCKET_BUFFER_SIZE];
414  int size;
415  struct SOCKET_BUFFER *next;
416 } socket_buffer;
417 
418 
419 typedef struct HTTP_HEADER {
420  token header;
421  token value;
422  struct HTTP_HEADER * next;
423 } http_header;
424 
425 
426 typedef struct HTTP_STATUS_LINE{
427  token http_version;
428  token status_code;
429  token reason_phrase;
430 } http_status;
431 
432 
433 typedef struct HTTP_REQUEST_LINE {
434  token http_version;
435  uri_type request_uri;
436  token method;
437 } http_request;
438 
439 
443 typedef struct HTTP_MESSAGE {
444  http_status status;
445  http_request request;
446  http_header * header_list;
447  token content;
448 } http_message;
449 #endif
450 
451 
452 #if 0
453 int transferHTTP(
454  char *request,
455  char *toSend,
456  int toSendSize,
457  char **out,
458  char *Url);
459 
460 
461 int transferHTTPRaw(
462  char *toSend,
463  int toSendSize,
464  char **out,
465  char *URL);
466 
467 
471 int transferHTTPparsedURL(
472  char *request,
473  char *toSend,
474  int toSendSize,
475  char **out,
476  uri_type *URL);
477 
478 
484 void currentTmToHttpDate(
485  char *out);
486 
487 
488 int parse_http_response(
489  char *in,
490  http_message *out,
491  int max_len);
492 
493 
494 int parse_http_request(
495  char *in,
496  http_message *out,
497  int max_len);
498 
499 
500 void print_http_message(
501  http_message *message);
502 
503 
504 int search_for_header(
505  http_message *in,
506  char *header,
507  token *out_value);
508 
509 
510 void print_status_line(
511  http_status *in);
512 
513 
514 void print_request_line(
515  http_request *in);
516 
517 
518 int parse_http_line(
519  char *in,
520  int max_size);
521 
522 
523 int parse_not_LWS(
524  char *in,
525  token *out,
526  int max_size);
527 
528 
529 int parse_LWS(
530  char *in,
531  int max_size);
532 
533 
534 size_t write_bytes(
535  int fd,
536  char *bytes,
537  size_t n,
538  int timeout);
539 
540 
541 void free_http_message(
542  http_message *message);
543 
544 
545 #endif
546 
547 
548 #ifdef __cplusplus
549 }
550 #endif
551 
552 
553 #endif /* GENLIB_NET_URI_H */
554 
int copy_URL_list(URL_list *in, URL_list *out)
Copies one URL_list into another.
Definition: uri.c:214
char * URLs
Definition: uri.h:153
Represents a host port: e.g. "127.127.0.1:80" text is a token pointing to the full string representat...
Definition: uri.h:126
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:136
int parse_uri(const char *in, size_t max, uri_type *out)
Parses a uri as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
Definition: uri.c:706
int remove_dots(char *in, size_t size)
Removes ".", and ".." from a path.
Definition: uri.c:526
int parse_uri_and_unescape(char *in, size_t max, uri_type *out)
Same as parse_uri(), except that all strings are unescaped (XX replaced by chars).
Definition: uri.c:753
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA. "char *" URLs holds dynamic memory.
Definition: uri.h:149
int token_string_cmp(token *in1, char *in2)
Compares a null terminated string to a token (exact).
Definition: uri.c:299
void print_uri(uri_type *in)
Function useful in debugging for printing a parsed uri.
Definition: uri.c:265
token text
Definition: uri.h:128
struct TOKEN token
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
Provides a platform independent way to include TCP/IP types and functions.
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
Definition: uri.h:117
struct URI uri_type
Represents a URI used in parse_uri and elsewhere.
int token_cmp(token *in1, token *in2)
Compares two tokens.
Definition: uri.c:308
struct HOSTPORT hostport_type
Represents a host port: e.g. "127.127.0.1:80" text is a token pointing to the full string representat...
void print_token(token *in)
Function useful in debugging for printing a token.
Definition: uri.c:276
char * resolve_rel_url(char *base_url, char *rel_url)
resolves a relative url with a base url returning a NEW (dynamically allocated with malloc) full url...
Definition: uri.c:592
int replace_escaped(char *in, size_t index, size_t *max)
Replaces an escaped sequence with its unescaped version as in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs)
Definition: uri.c:135
int token_string_casecmp(token *in1, const char *in2)
Compares buffer in the token object with the buffer in in2.
Definition: uri.c:289
int parse_token(char *in, token *out, int max_size)
struct URL_LIST URL_list
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA. "char *" URLs holds dynamic memory.
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:93
Defines constants that for some reason are not defined on some systems.
void free_URL_list(URL_list *list)
Frees the memory associated with a URL_list.
Definition: uri.c:252
int remove_escaped_chars(char *in, size_t *size)
Removes http escaped characters such as: "%20" and replaces them with their character representation...