libUPnP 1.8.4
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
70extern "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
89enum hostType {
90 HOSTNAME,
91 IPv4address
92};
93
94enum 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
117typedef struct TOKEN {
118 const char *buff;
119 size_t size;
121
126typedef struct HOSTPORT {
129 /* Network Byte Order */
130 struct sockaddr_storage IPaddress;
132
136typedef struct URI{
137 enum uriType type;
138 token scheme;
139 enum pathType path_type;
140 token pathquery;
141 token fragment;
142 hostport_type hostport;
144
149typedef struct URL_LIST {
151 size_t size;
153 char *URLs;
155 uri_type *parsedURLs;
157
172 char *in,
174 size_t index,
176 size_t *max);
177
189int copy_URL_list(
191 URL_list *in,
193 URL_list *out);
194
201void free_URL_list(
203 URL_list *list);
204
208#ifdef DEBUG
209void print_uri(
211 uri_type *in);
212#else
213static UPNP_INLINE void print_uri(uri_type *in)
214{
215 return;
216 in = in;
217}
218#endif
219
223#ifdef DEBUG
224void print_token(
226 token *in);
227#else
228static 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
261 token *in1,
263 char *in2);
264
273int token_cmp(
275 token *in1,
277 token *in2);
278
290 char *in,
292 size_t *size);
293
316int remove_dots(
318 char *in,
320 size_t size);
321
336char *resolve_rel_url(
338 char *base_url,
340 char *rel_url);
341
354int 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
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
412typedef struct SOCKET_BUFFER{
413 char buff[SOCKET_BUFFER_SIZE];
414 int size;
415 struct SOCKET_BUFFER *next;
416} socket_buffer;
417
418
419typedef struct HTTP_HEADER {
420 token header;
421 token value;
422 struct HTTP_HEADER * next;
423} http_header;
424
425
426typedef struct HTTP_STATUS_LINE{
427 token http_version;
428 token status_code;
429 token reason_phrase;
430} http_status;
431
432
433typedef struct HTTP_REQUEST_LINE {
434 token http_version;
435 uri_type request_uri;
436 token method;
437} http_request;
438
439
443typedef 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
453int transferHTTP(
454 char *request,
455 char *toSend,
456 int toSendSize,
457 char **out,
458 char *Url);
459
460
461int transferHTTPRaw(
462 char *toSend,
463 int toSendSize,
464 char **out,
465 char *URL);
466
467
471int transferHTTPparsedURL(
472 char *request,
473 char *toSend,
474 int toSendSize,
475 char **out,
476 uri_type *URL);
477
478
484void currentTmToHttpDate(
485 char *out);
486
487
488int parse_http_response(
489 char *in,
490 http_message *out,
491 int max_len);
492
493
494int parse_http_request(
495 char *in,
496 http_message *out,
497 int max_len);
498
499
500void print_http_message(
501 http_message *message);
502
503
504int search_for_header(
505 http_message *in,
506 char *header,
507 token *out_value);
508
509
510void print_status_line(
511 http_status *in);
512
513
514void print_request_line(
515 http_request *in);
516
517
518int parse_http_line(
519 char *in,
520 int max_size);
521
522
523int parse_not_LWS(
524 char *in,
525 token *out,
526 int max_size);
527
528
529int parse_LWS(
530 char *in,
531 int max_size);
532
533
534size_t write_bytes(
535 int fd,
536 char *bytes,
537 size_t n,
538 int timeout);
539
540
541void 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
Defines constants that for some reason are not defined on some systems.
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:99
Provides a platform independent way to include TCP/IP types and functions.
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
token text
Definition: uri.h:128
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
Definition: uri.h:117
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:136
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA....
Definition: uri.h:149
char * URLs
Definition: uri.h:153
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:593
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....
int token_string_cmp(token *in1, char *in2)
Compares a null terminated string to a token (exact).
Definition: uri.c:299
int copy_URL_list(URL_list *in, URL_list *out)
Copies one URL_list into another.
Definition: uri.c:214
struct TOKEN token
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
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....
Definition: uri.c:135
int remove_dots(char *in, size_t size)
Removes ".", and ".." from a path.
Definition: uri.c:527
int token_cmp(token *in1, token *in2)
Compares two tokens.
Definition: uri.c:308
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:759
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...
int token_string_casecmp(token *in1, const char *in2)
Compares buffer in the token object with the buffer in in2.
Definition: uri.c:289
struct URI uri_type
Represents a URI used in parse_uri and elsewhere.
int remove_escaped_chars(char *in, size_t *size)
Removes http escaped characters such as: "%20" and replaces them with their character representation....
void print_uri(uri_type *in)
Function useful in debugging for printing a parsed uri.
Definition: uri.c:265
void print_token(token *in)
Function useful in debugging for printing a token.
Definition: uri.c:276
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:707
void free_URL_list(URL_list *list)
Frees the memory associated with a URL_list.
Definition: uri.c:252