uri.h
Go to the documentation of this file.
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#ifndef GENLIB_NET_URI_H
#define GENLIB_NET_URI_H
#if !defined(_WIN32)
#include <sys/param.h>
#endif
#include "UpnpGlobal.h" /* for */
#include "UpnpInet.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#ifdef _WIN32
#if !defined(UPNP_USE_MSVCPP) && !defined(UPNP_USE_BCBPP)
/* VC Winsocks2 includes these functions */
#include "inet_pton.h"
#endif
#else
#include <netdb.h> /* for struct addrinfo */
#endif
#ifdef _WIN32
#define strncasecmp strnicmp
#else
/* Other systems have strncasecmp */
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define HTTP_DATE_LENGTH 37
#define SEPARATORS "()<>@,;:\\\"/[]?={} \t"
#define MARK "-_.!~*'()"
#define RESERVED ";/?:@&=+$,{}"
#define HTTP_SUCCESS 1
#define FALSE 0
#define TAB 9
#define CR 13
#define LF 10
#define SOCKET_BUFFER_SIZE 5000
enum hostType {
HOSTNAME,
IPv4address
};
enum pathType {
ABS_PATH,
REL_PATH,
OPAQUE_PART
};
#ifdef _WIN32
/* there is a conflict in windows with other symbols. */
enum uriType {
absolute,
relative
};
#else
enum uriType {
ABSOLUTE,
RELATIVE
};
#endif
typedef struct TOKEN {
const char *buff;
size_t size;
} token;
typedef struct HOSTPORT {
token text;
/* Network Byte Order */
struct sockaddr_storage IPaddress;
} hostport_type;
typedef struct URI{
enum uriType type;
token scheme;
enum pathType path_type;
token pathquery;
token fragment;
hostport_type hostport;
} uri_type;
typedef struct URL_LIST {
size_t size;
char *URLs;
uri_type *parsedURLs;
} URL_list;
int replace_escaped(
char *in,
size_t index,
size_t *max);
int copy_URL_list(
URL_list *in,
URL_list *out);
void free_URL_list(
URL_list *list);
#ifdef DEBUG
void print_uri(
uri_type *in);
#else
static UPNP_INLINE void print_uri(uri_type *in)
{
return;
in = in;
}
#endif
#ifdef DEBUG
void print_token(
token *in);
#else
static UPNP_INLINE void print_token(
token *in)
{
return;
in = in;
}
#endif
int token_string_casecmp(
token *in1,
const char *in2);
int token_string_cmp(
token *in1,
char *in2);
int token_cmp(
token *in1,
token *in2);
int remove_escaped_chars(
char *in,
size_t *size);
int remove_dots(
char *in,
size_t size);
char *resolve_rel_url(
char *base_url,
char *rel_url);
int parse_uri(
const char *in,
size_t max,
uri_type *out);
int parse_uri_and_unescape(
char *in,
size_t max,
uri_type *out);
int parse_token(
char *in,
token *out,
int max_size);
/* Commented #defines, functions and typdefs */
#if 0
#define HTTP_E_BAD_URL UPNP_E_INVALID_URL
#define HTTP_E_READ_SOCKET UPNP_E_SOCKET_READ
#define HTTP_E_BIND_SOCKET UPNP_E_SOCKET_BIND
#define HTTP_E_WRITE_SOCKET UPNP_E_SOCKET_WRITE
#define HTTP_E_CONNECT_SOCKET UPNP_E_SOCKET_CONNECT
#define HTTP_E_SOCKET UPNP_E_OUTOF_SOCKET
#define HTTP_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE
#define HTTP_E_BAD_REQUEST UPNP_E_BAD_REQUEST
#define HTTP_E_BAD_IP_ADDRESS UPNP_E_INVALID_URL
#define RESPONSE_TIMEOUT 30
#endif
#if 0
typedef struct SOCKET_BUFFER{
char buff[SOCKET_BUFFER_SIZE];
int size;
struct SOCKET_BUFFER *next;
} socket_buffer;
typedef struct HTTP_HEADER {
token header;
token value;
struct HTTP_HEADER * next;
} http_header;
typedef struct HTTP_STATUS_LINE{
token http_version;
token status_code;
token reason_phrase;
} http_status;
typedef struct HTTP_REQUEST_LINE {
token http_version;
uri_type request_uri;
token method;
} http_request;
typedef struct HTTP_MESSAGE {
http_status status;
http_request request;
http_header * header_list;
token content;
} http_message;
#endif
#if 0
int transferHTTP(
char *request,
char *toSend,
int toSendSize,
char **out,
char *Url);
int transferHTTPRaw(
char *toSend,
int toSendSize,
char **out,
char *URL);
int transferHTTPparsedURL(
char *request,
char *toSend,
int toSendSize,
char **out,
uri_type *URL);
void currentTmToHttpDate(
char *out);
int parse_http_response(
char *in,
http_message *out,
int max_len);
int parse_http_request(
char *in,
http_message *out,
int max_len);
void print_http_message(
http_message *message);
int search_for_header(
http_message *in,
char *header,
token *out_value);
void print_status_line(
http_status *in);
void print_request_line(
http_request *in);
int parse_http_line(
char *in,
int max_size);
int parse_not_LWS(
char *in,
token *out,
int max_size);
int parse_LWS(
char *in,
int max_size);
size_t write_bytes(
int fd,
char *bytes,
size_t n,
int timeout);
void free_http_message(
http_message *message);
#endif
#ifdef __cplusplus
}
#endif
#endif /* GENLIB_NET_URI_H */
copy_URL_list
int copy_URL_list(URL_list *in, URL_list *out)
Copies one URL_list into another.
Definition: uri.c:214
URL_LIST::URLs
char * URLs
Definition: uri.h:153
HOSTPORT
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
URI
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:136
parse_uri
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
remove_dots
int remove_dots(char *in, size_t size)
Removes ".", and ".." from a path.
Definition: uri.c:527
parse_uri_and_unescape
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
URL_LIST
Represents a list of URLs as in the "callback" header of SUBSCRIBE message in GENA. "char *" URLs holds dynamic memory.
Definition: uri.h:149
token_string_cmp
int token_string_cmp(token *in1, char *in2)
Compares a null terminated string to a token (exact).
Definition: uri.c:299
print_uri
void print_uri(uri_type *in)
Function useful in debugging for printing a parsed uri.
Definition: uri.c:265
HOSTPORT::text
token text
Definition: uri.h:128
token
struct TOKEN token
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
UpnpInet.h
Provides a platform independent way to include TCP/IP types and functions.
TOKEN
Buffer used in parsinghttp messages, urls, etc. generally this simply holds a pointer into a larger a...
Definition: uri.h:117
uri_type
struct URI uri_type
Represents a URI used in parse_uri and elsewhere.
token_cmp
int token_cmp(token *in1, token *in2)
Compares two tokens.
Definition: uri.c:308
hostport_type
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...
print_token
void print_token(token *in)
Function useful in debugging for printing a token.
Definition: uri.c:276
resolve_rel_url
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
replace_escaped
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
token_string_casecmp
int token_string_casecmp(token *in1, const char *in2)
Compares buffer in the token object with the buffer in in2.
Definition: uri.c:289
parse_token
int parse_token(char *in, token *out, int max_size)
URL_list
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.
UPNP_INLINE
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:99
UpnpGlobal.h
Defines constants that for some reason are not defined on some systems.
free_URL_list
void free_URL_list(URL_list *list)
Frees the memory associated with a URL_list.
Definition: uri.c:252
remove_escaped_chars
int remove_escaped_chars(char *in, size_t *size)
Removes http escaped characters such as: "%20" and replaces them with their character representation...