libUPnP  1.14.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 #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  #if !defined(UPNP_USE_MSVCPP) && !defined(UPNP_USE_BCBPP)
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 SOCKET_BUFFER_SIZE 5000
84 
85 enum hostType {
86  HOSTNAME,
87  IPv4address
88 };
89 
90 enum pathType {
91  ABS_PATH,
92  REL_PATH,
93  OPAQUE_PART
94 };
95 
96 #ifdef _WIN32
97  /* there is a conflict in windows with other symbols. */
98  enum uriType {
99  absolute,
100  relative
101  };
102 #else
103  enum uriType {
104  ABSOLUTE,
105  RELATIVE
106  };
107 #endif
108 
113 typedef struct TOKEN {
114  const char *buff;
115  size_t size;
116 } token;
117 
122 typedef struct HOSTPORT {
125  /* Network Byte Order */
126  struct sockaddr_storage IPaddress;
127 } hostport_type;
128 
132 typedef struct URI{
133  enum uriType type;
134  token scheme;
135  enum pathType path_type;
136  token pathquery;
137  token fragment;
138  hostport_type hostport;
139 } uri_type;
140 
145 typedef struct URL_LIST {
147  size_t size;
149  char *URLs;
151  uri_type *parsedURLs;
152 } URL_list;
153 
166 int replace_escaped(
168  char *in,
170  size_t index,
172  size_t *max);
173 
185 int copy_URL_list(
187  URL_list *in,
189  URL_list *out);
190 
197 void free_URL_list(
199  URL_list *list);
200 
204 #ifdef DEBUG
205 void print_uri(
207  uri_type *in);
208 #else
209 #define print_uri(in) do {} while (0)
210 #endif
211 
215 #ifdef DEBUG
216 void print_token(
218  token *in);
219 #else
220 #define print_token(in) do {} while (0)
221 #endif
222 
233  token *in1,
235  const char *in2);
236 
245 int token_string_cmp(
247  token *in1,
249  char *in2);
250 
259 int token_cmp(
261  token *in1,
263  token *in2);
264 
276  char *in,
278  size_t *size);
279 
302 int remove_dots(
304  char *in,
306  size_t size);
307 
322 char *resolve_rel_url(
324  char *base_url,
326  char *rel_url);
327 
340 int parse_uri(
342  const char *in,
344  size_t max,
346  uri_type *out);
347 
358  char *in,
360  size_t max,
362  uri_type *out);
363 
369 int parse_token(
371  char *in,
373  token *out,
375  int max_size);
376 
377 /* Commented #defines, functions and typdefs */
378 
379 #if 0
380 #define HTTP_E_BAD_URL UPNP_E_INVALID_URL
381 #define HTTP_E_READ_SOCKET UPNP_E_SOCKET_READ
382 #define HTTP_E_BIND_SOCKET UPNP_E_SOCKET_BIND
383 #define HTTP_E_WRITE_SOCKET UPNP_E_SOCKET_WRITE
384 #define HTTP_E_CONNECT_SOCKET UPNP_E_SOCKET_CONNECT
385 #define HTTP_E_SOCKET UPNP_E_OUTOF_SOCKET
386 #define HTTP_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE
387 #define HTTP_E_BAD_REQUEST UPNP_E_BAD_REQUEST
388 #define HTTP_E_BAD_IP_ADDRESS UPNP_E_INVALID_URL
389 
390 #define RESPONSE_TIMEOUT 30
391 #endif
392 
393 #if 0
394 
398 typedef struct SOCKET_BUFFER{
399  char buff[SOCKET_BUFFER_SIZE];
400  int size;
401  struct SOCKET_BUFFER *next;
402 } socket_buffer;
403 
404 
405 typedef struct HTTP_HEADER {
406  token header;
407  token value;
408  struct HTTP_HEADER * next;
409 } http_header;
410 
411 
412 typedef struct HTTP_STATUS_LINE{
413  token http_version;
414  token status_code;
415  token reason_phrase;
416 } http_status;
417 
418 
419 typedef struct HTTP_REQUEST_LINE {
420  token http_version;
421  uri_type request_uri;
422  token method;
423 } http_request;
424 
425 
429 typedef struct HTTP_MESSAGE {
430  http_status status;
431  http_request request;
432  http_header * header_list;
433  token content;
434 } http_message;
435 #endif
436 
437 
438 #if 0
439 int transferHTTP(
440  char *request,
441  char *toSend,
442  int toSendSize,
443  char **out,
444  char *Url);
445 
446 
447 int transferHTTPRaw(
448  char *toSend,
449  int toSendSize,
450  char **out,
451  char *URL);
452 
453 
457 int transferHTTPparsedURL(
458  char *request,
459  char *toSend,
460  int toSendSize,
461  char **out,
462  uri_type *URL);
463 
464 
470 void currentTmToHttpDate(
471  char *out);
472 
473 
474 int parse_http_response(
475  char *in,
476  http_message *out,
477  int max_len);
478 
479 
480 int parse_http_request(
481  char *in,
482  http_message *out,
483  int max_len);
484 
485 
486 void print_http_message(
487  http_message *message);
488 
489 
490 int search_for_header(
491  http_message *in,
492  char *header,
493  token *out_value);
494 
495 
496 void print_status_line(
497  http_status *in);
498 
499 
500 void print_request_line(
501  http_request *in);
502 
503 
504 int parse_http_line(
505  char *in,
506  int max_size);
507 
508 
509 int parse_not_LWS(
510  char *in,
511  token *out,
512  int max_size);
513 
514 
515 int parse_LWS(
516  char *in,
517  int max_size);
518 
519 
520 size_t write_bytes(
521  int fd,
522  char *bytes,
523  size_t n,
524  int timeout);
525 
526 
527 void free_http_message(
528  http_message *message);
529 
530 
531 #endif
532 
533 
534 #ifdef __cplusplus
535 }
536 #endif
537 
538 
539 #endif /* GENLIB_NET_URI_H */
540 
int copy_URL_list(URL_list *in, URL_list *out)
Copies one URL_list into another.
Definition: uri.c:206
char * URLs
Definition: uri.h:149
Represents a host port: e.g. "127.127.0.1:80" text is a token pointing to the full string representat...
Definition: uri.h:122
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:132
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:734
int remove_dots(char *in, size_t size)
Removes ".", and ".." from a path.
Definition: uri.c:520
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:786
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA. "char *" URLs holds dynamic memory.
Definition: uri.h:145
int token_string_cmp(token *in1, char *in2)
Compares a null terminated string to a token (exact).
Definition: uri.c:292
void print_uri(uri_type *in)
Function useful in debugging for printing a parsed uri.
token text
Definition: uri.h:124
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:113
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:301
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.
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:588
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:128
int token_string_casecmp(token *in1, const char *in2)
Compares buffer in the token object with the buffer in in2.
Definition: uri.c:283
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.
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:250
int remove_escaped_chars(char *in, size_t *size)
Removes http escaped characters such as: "%20" and replaces them with their character representation...
Definition: uri.c:496