libUPnP  1.8.0
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 
33 #ifndef GENLIB_NET_URI_H
34 #define GENLIB_NET_URI_H
35 
36 
41 #if !defined(WIN32)
42  #include <sys/param.h>
43 #endif
44 
45 
46 #include "UpnpGlobal.h" /* for */
47 #include "UpnpInet.h"
48 
49 
50 #include <ctype.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #if defined(BSD)
56  #include <sys/socket.h>
57 #endif
58 #include <sys/types.h>
59 #include <time.h>
60 
61 
62 #ifdef WIN32
63 #else
64  #include <arpa/inet.h> /* for inet_pton() */
65  #include <netdb.h> /* for struct addrinfo */
66 #endif
67 
68 
69 #ifdef WIN32
70  #define strncasecmp strnicmp
71 #else
72  /* Other systems have strncasecmp */
73 #endif
74 
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
80 
81 #define HTTP_DATE_LENGTH 37 // length for HTTP DATE:
82  //"DATE: Sun, 01 Jul 2000 08:15:23 GMT<cr><lf>"
83 #define SEPARATORS "()<>@,;:\\\"/[]?={} \t"
84 #define MARK "-_.!~*'()"
85 #define RESERVED ";/?:@&=+$,{}" //added {} for compatibility
86 #define HTTP_SUCCESS 1
87 
88 
89 #define FALSE 0
90 #define TAB 9
91 #define CR 13
92 #define LF 10
93 #define SOCKET_BUFFER_SIZE 5000
94 
95 
96 enum hostType {
97  HOSTNAME,
98  IPv4address
99 };
100 
101 
102 enum pathType {
103  ABS_PATH,
104  REL_PATH,
105  OPAQUE_PART
106 };
107 
108 
109 #ifdef WIN32
110  /* there is a conflict in windows with other symbols */
111  enum uriType {
112  absolute,
113  relative
114  };
115 #else
116  enum uriType {
117  ABSOLUTE,
118  RELATIVE
119  };
120 #endif
121 
122 
127 typedef struct TOKEN {
128  const char *buff;
129  size_t size;
130 } token;
131 
132 
137 typedef struct HOSTPORT {
140  /* Network Byte Order */
141  struct sockaddr_storage IPaddress;
142 } hostport_type;
143 
144 
148 typedef struct URI{
149  enum uriType type;
150  token scheme;
151  enum pathType path_type;
152  token pathquery;
153  token fragment;
154  hostport_type hostport;
155 } uri_type;
156 
157 
162 typedef struct URL_LIST {
164  int size;
166  char *URLs;
168  uri_type *parsedURLs;
169 } URL_list;
170 
171 
184 int replace_escaped(
186  char *in,
188  int index,
190  size_t *max);
191 
192 
204 int copy_URL_list(
206  URL_list *in,
208  URL_list *out);
209 
216 void free_URL_list(
218  URL_list *list);
219 
220 
224 #ifdef DEBUG
225 void print_uri(
227  uri_type *in);
228 #else
229 static UPNP_INLINE void print_uri(uri_type *in) {}
230 #endif
231 
232 
236 #ifdef DEBUG
237 void print_token(
239  token *in);
240 #else
241 static UPNP_INLINE void print_token(token * in) {}
242 #endif
243 
244 
255  token *in1,
257  char *in2);
258 
259 
268 int token_string_cmp(
270  token *in1,
272  char *in2);
273 
274 
283 int token_cmp(
285  token *in1,
287  token *in2);
288 
289 
297 int parse_hostport(
299  const char *in,
301  int max,
304  hostport_type *out);
305 
306 
318  char *in,
320  size_t *size);
321 
322 
345 int remove_dots(
347  char *in,
349  size_t size);
350 
351 
366 char *resolve_rel_url(
368  char *base_url,
370  char *rel_url);
371 
372 
385 int parse_uri(
387  const char *in,
389  int max,
391  uri_type *out);
392 
393 
404  char *in,
406  int max,
408  uri_type *out);
409 
410 
416 int parse_token(
418  char *in,
420  token *out,
422  int max_size);
423 
424 
425 /* Commented #defines, functions and typdefs */
426 
427 #if 0
428 #define HTTP_E_BAD_URL UPNP_E_INVALID_URL
429 #define HTTP_E_READ_SOCKET UPNP_E_SOCKET_READ
430 #define HTTP_E_BIND_SOCKET UPNP_E_SOCKET_BIND
431 #define HTTP_E_WRITE_SOCKET UPNP_E_SOCKET_WRITE
432 #define HTTP_E_CONNECT_SOCKET UPNP_E_SOCKET_CONNECT
433 #define HTTP_E_SOCKET UPNP_E_OUTOF_SOCKET
434 #define HTTP_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE
435 #define HTTP_E_BAD_REQUEST UPNP_E_BAD_REQUEST
436 #define HTTP_E_BAD_IP_ADDRESS UPNP_E_INVALID_URL
437 
438 #define RESPONSE_TIMEOUT 30
439 #endif
440 
441 #if 0
442 
446 typedef struct SOCKET_BUFFER{
447  char buff[SOCKET_BUFFER_SIZE];
448  int size;
449  struct SOCKET_BUFFER *next;
450 } socket_buffer;
451 
452 
453 typedef struct HTTP_HEADER {
454  token header;
455  token value;
456  struct HTTP_HEADER * next;
457 } http_header;
458 
459 
460 typedef struct HTTP_STATUS_LINE{
461  token http_version;
462  token status_code;
463  token reason_phrase;
464 } http_status;
465 
466 
467 typedef struct HTTP_REQUEST_LINE {
468  token http_version;
469  uri_type request_uri;
470  token method;
471 } http_request;
472 
473 
477 typedef struct HTTP_MESSAGE {
478  http_status status;
479  http_request request;
480  http_header * header_list;
481  token content;
482 } http_message;
483 #endif
484 
485 
486 #if 0
487 int transferHTTP(
488  char *request,
489  char *toSend,
490  int toSendSize,
491  char **out,
492  char *Url);
493 
494 
495 int transferHTTPRaw(
496  char *toSend,
497  int toSendSize,
498  char **out,
499  char *URL);
500 
501 
505 int transferHTTPparsedURL(
506  char *request,
507  char *toSend,
508  int toSendSize,
509  char **out,
510  uri_type *URL);
511 
512 
518 void currentTmToHttpDate(
519  char *out);
520 
521 
522 int parse_http_response(
523  char *in,
524  http_message *out,
525  int max_len);
526 
527 
528 int parse_http_request(
529  char *in,
530  http_message *out,
531  int max_len);
532 
533 
534 void print_http_message(
535  http_message *message);
536 
537 
538 int search_for_header(
539  http_message *in,
540  char *header,
541  token *out_value);
542 
543 
544 void print_status_line(
545  http_status *in);
546 
547 
548 void print_request_line(
549  http_request *in);
550 
551 
552 int parse_http_line(
553  char *in,
554  int max_size);
555 
556 
557 int parse_not_LWS(
558  char *in,
559  token *out,
560  int max_size);
561 
562 
563 int parse_LWS(
564  char *in,
565  int max_size);
566 
567 
568 size_t write_bytes(
569  int fd,
570  char *bytes,
571  size_t n,
572  int timeout);
573 
574 
575 void free_http_message(
576  http_message *message);
577 
578 
579 #endif
580 
581 
582 #ifdef __cplusplus
583 }
584 #endif
585 
586 
587 #endif /* GENLIB_NET_URI_H */
588 
int token_string_casecmp(token *in1, char *in2)
Compares buffer in the token object with the buffer in in2.
Definition: uri.c:292
int copy_URL_list(URL_list *in, URL_list *out)
Copies one URL_list into another.
Definition: uri.c:212
int parse_uri_and_unescape(char *in, int max, uri_type *out)
Same as parse_uri(), except that all strings are unescaped (XX replaced by chars).
Definition: uri.c:759
int parse_uri(const char *in, int max, uri_type *out)
Parses a uri as defined in http://www.ietf.org/rfc/rfc2396.txt (RFC explaining URIs).
Definition: uri.c:708
char * URLs
Definition: uri.h:166
Represents a host port: e.g. "127.127.0.1:80" text is a token pointing to the full string representat...
Definition: uri.h:137
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:148
int remove_dots(char *in, size_t size)
Removes ".", and ".." from a path.
Definition: uri.c:530
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA. "char *" URLs holds dynamic memory.
Definition: uri.h:162
int token_string_cmp(token *in1, char *in2)
Compares a null terminated string to a token (exact).
Definition: uri.c:304
void print_uri(uri_type *in)
Function useful in debugging for printing a parsed uri.
Definition: uri.c:268
token text
Definition: uri.h:139
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:127
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:316
int replace_escaped(char *in, int 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:132
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:279
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:596
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:91
int parse_hostport(const char *in, int max, hostport_type *out)
Parses a string representing a host and port (e.g. "127.127.0.1:80" or "localhost") and fills out a h...
Definition: uri.c:326
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:255
int remove_escaped_chars(char *in, size_t *size)
Removes http escaped characters such as: "%20" and replaces them with their character representation...