httpparser.h
Go to the documentation of this file.
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (c) 2012 France Telecom 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_HTTP_HTTPPARSER_H
#define GENLIB_NET_HTTP_HTTPPARSER_H
#include "LinkedList.h"
#include "membuffer.h"
#include "upnputil.h"
#include "uri.h"
/* private types */
/* scanner */
/* Used to represent different types of tokens in input. */
typedef enum
{
TT_IDENTIFIER,
TT_WHITESPACE,
TT_CRLF,
TT_CTRL,
TT_SEPARATOR,
TT_QUOTEDSTRING
} token_type_t;
typedef struct
{
membuffer *msg;
size_t cursor;
int entire_msg_loaded;
} scanner_t;
typedef enum
{
POS_REQUEST_LINE,
POS_RESPONSE_LINE,
POS_HEADERS,
POS_ENTITY,
POS_COMPLETE
} parser_pos_t;
#define ENTREAD_DETERMINE_READ_METHOD 1
#define ENTREAD_USING_CLEN 2
#define ENTREAD_USING_CHUNKED 3
#define ENTREAD_UNTIL_CLOSE 4
#define ENTREAD_CHUNKY_BODY 5
#define ENTREAD_CHUNKY_HEADERS 6
/* end of private section. */
/* method in a HTTP request.
* IMPORTANT: The enum values of the standard HTTP method should match
* those of Upnp_HttpMethod enum defined in upnp.h */
typedef enum
{
HTTPMETHOD_PUT = UPNP_HTTPMETHOD_PUT,
HTTPMETHOD_DELETE = UPNP_HTTPMETHOD_DELETE,
HTTPMETHOD_GET = UPNP_HTTPMETHOD_GET,
HTTPMETHOD_HEAD = UPNP_HTTPMETHOD_HEAD,
HTTPMETHOD_POST = UPNP_HTTPMETHOD_POST,
HTTPMETHOD_MPOST,
HTTPMETHOD_SUBSCRIBE,
HTTPMETHOD_UNSUBSCRIBE,
HTTPMETHOD_NOTIFY,
HTTPMETHOD_MSEARCH,
HTTPMETHOD_UNKNOWN,
SOAPMETHOD_POST,
HTTPMETHOD_SIMPLEGET
} http_method_t;
/* different types of HTTP headers */
#define HDR_UNKNOWN -1
#define HDR_CACHE_CONTROL 1
#define HDR_CALLBACK 2
#define HDR_CONTENT_LENGTH 3
#define HDR_CONTENT_TYPE 4
#define HDR_DATE 5
#define HDR_EXT 6
#define HDR_HOST 7
/*define HDR_IF_MODIFIED_SINCE 8 */
/*define HDR_IF_UNMODIFIED_SINCE 9 */
/*define HDR_LAST_MODIFIED 10 */
#define HDR_LOCATION 11
#define HDR_MAN 12
#define HDR_MX 13
#define HDR_NT 14
#define HDR_NTS 15
#define HDR_SERVER 16
#define HDR_SEQ 17
#define HDR_SID 18
#define HDR_SOAPACTION 19
#define HDR_ST 20
#define HDR_TIMEOUT 21
#define HDR_TRANSFER_ENCODING 22
#define HDR_USN 23
#define HDR_USER_AGENT 24
/* Adding new header difinition */
#define HDR_ACCEPT 25
#define HDR_ACCEPT_ENCODING 26
#define HDR_ACCEPT_CHARSET 27
#define HDR_ACCEPT_LANGUAGE 28
#define HDR_ACCEPT_RANGE 29
#define HDR_CONTENT_ENCODING 30
#define HDR_CONTENT_LANGUAGE 31
#define HDR_CONTENT_LOCATION 32
#define HDR_CONTENT_RANGE 33
#define HDR_IF_RANGE 34
#define HDR_RANGE 35
#define HDR_TE 36
typedef enum
{
PARSE_SUCCESS = 0,
PARSE_INCOMPLETE,
PARSE_INCOMPLETE_ENTITY,
PARSE_FAILURE,
PARSE_OK,
PARSE_NO_MATCH,
PARSE_CONTINUE_1
} parse_status_t;
typedef struct
{
memptr name;
int name_id;
membuffer value;
/* private. */
membuffer name_buf;
} http_header_t;
typedef struct
{
int initialized;
http_method_t method;
uri_type uri;
http_method_t request_method;
int status_code;
membuffer status_msg;
size_t amount_discarded;
/* fields used in both request or response messages. */
int is_request;
/* http major version. */
int major_version;
/* http minor version. */
int minor_version;
LinkedList headers;
memptr entity;
/* private fields. */
membuffer msg;
char *urlbuf;
} http_message_t;
typedef struct
{
http_message_t msg;
int http_error_code;
int valid_ssdp_notify_hack;
/* private data -- don't touch. */
parser_pos_t position;
int ent_position;
unsigned int content_length;
size_t chunk_size;
size_t entity_start_position;
scanner_t scanner;
} http_parser_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/************************************************************************
* Function : httpmsg_init
*
* Parameters :
* INOUT http_message_t* msg ; HTTP Message Object
*
* Description : Initialize and allocate memory for http message
*
* Return : void ;
*
* Note :
************************************************************************/
void httpmsg_init(http_message_t *msg);
/************************************************************************
* Function : httpmsg_destroy
*
* Parameters :
* INOUT http_message_t* msg ; HTTP Message Object
*
* Description : Free memory allocated for the http message
*
* Return : void ;
*
* Note :
************************************************************************/
void httpmsg_destroy(http_message_t *msg);
/************************************************************************
* Function : httpmsg_find_hdr_str
*
* Parameters :
* IN http_message_t* msg ; HTTP Message Object
* IN const char* header_name ; Header name to be compared with
*
* Description : Compares the header name with the header names stored
* in the linked list of messages
*
* Return : http_header_t* - Pointer to a header on success;
* NULL on failure
* Note :
************************************************************************/
http_header_t *httpmsg_find_hdr_str(
http_message_t *msg, const char *header_name);
/************************************************************************
* Function : httpmsg_find_hdr
*
* Parameters :
* IN http_message_t* msg ; HTTP Message Object
* IN int header_name_id ; Header Name ID to be compared with
* OUT memptr* value ; Buffer to get the ouput to.
*
* Description : Finds header from a list, with the given 'name_id'.
*
* Return : http_header_t* - Pointer to a header on success;
* NULL on failure
*
* Note :
************************************************************************/
http_header_t *httpmsg_find_hdr(
http_message_t *msg, int header_name_id, memptr *value);
/************************************************************************
* Function: parser_request_init
*
* Parameters:
* OUT http_parser_t* parser ; HTTP Parser object
*
* Description: Initializes parser object for a request
*
* Returns:
* void
************************************************************************/
void parser_request_init(http_parser_t *parser);
/************************************************************************
* Function: parser_response_init
*
* Parameters:
* OUT http_parser_t* parser ; HTTP Parser object
* IN http_method_t request_method ; Request method
*
* Description: Initializes parser object for a response
*
* Returns:
* void
************************************************************************/
void parser_response_init(http_parser_t *parser, http_method_t request_method);
/************************************************************************
* Function: parser_parse
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser object
*
* Description: The parser function. Depending on the position of the
* parser object the actual parsing function is invoked
*
* Returns:
* void
************************************************************************/
parse_status_t parser_parse(http_parser_t *parser);
/************************************************************************
* Function: parser_parse_responseline
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser object
*
* Description: Get HTTP Method, URL location and version information.
*
* Returns:
* PARSE_OK
* PARSE_SUCCESS
* PARSE_FAILURE
************************************************************************/
parse_status_t parser_parse_responseline(http_parser_t *parser);
/************************************************************************
* Function: parser_parse_headers
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser object
*
* Description: Get HTTP Method, URL location and version information.
*
* Returns:
* PARSE_OK
* PARSE_SUCCESS
* PARSE_FAILURE
************************************************************************/
parse_status_t parser_parse_headers(http_parser_t *parser);
/************************************************************************
* Function: parser_parse_entity
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser object
*
* Description: Determines method to read entity
*
* Returns:
* PARSE_OK
* PARSE_FAILURE
* PARSE_COMPLETE -- no more reading to do
************************************************************************/
parse_status_t parser_parse_entity(http_parser_t *parser);
/************************************************************************
* Function: parser_get_entity_read_method
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser object
*
* Description: Determines method to read entity
*
* Returns:
* PARSE_OK
* PARSE_FAILURE
* PARSE_COMPLETE -- no more reading to do
************************************************************************/
parse_status_t parser_get_entity_read_method(http_parser_t *parser);
/************************************************************************
* Function: parser_append
*
* Parameters:
* INOUT http_parser_t* parser ; HTTP Parser Object
* IN const char* buf ; buffer to be appended to the parser
* buffer
* IN size_t buf_length ; Size of the buffer
*
* Description: The parser function. Depending on the position of the
* parser object the actual parsing function is invoked
*
* Returns:
* void
************************************************************************/
parse_status_t parser_append(
http_parser_t *parser, const char *buf, size_t buf_length);
/************************************************************************
* Function: matchstr
*
* Parameters:
* IN char *str ; String to be matched
* IN size_t slen ; Length of the string
* IN const char* fmt ; Pattern format
* ...
*
* Description: Matches a variable parameter list with a string
* and takes actions based on the data type specified.
*
* Returns:
* PARSE_OK
* PARSE_NO_MATCH -- failure to match pattern 'fmt'
* PARSE_FAILURE -- 'str' is bad input
************************************************************************/
parse_status_t matchstr(char *str, size_t slen, const char *fmt, ...);
/************************************************************************
* Function: raw_to_int
*
* Parameters:
* IN memptr* raw_value ; Buffer to be converted
* IN int base ; Base to use for conversion
*
* Description: Converts raw character data to long-integer value
*
* Returns:
* int
************************************************************************/
int raw_to_int(memptr *raw_value, int base);
/************************************************************************
* Function: raw_find_str
*
* Parameters:
* IN memptr* raw_value ; Buffer containg the string
* IN const char* str ; Substring to be found
*
* Description: Find a substring from raw character string buffer
*
* Side effects: raw_value is transformed to lowercase.
*
* Returns:
* int - index at which the substring is found.
************************************************************************/
int raw_find_str(memptr *raw_value, const char *str);
/************************************************************************
* Function: method_to_str
*
* Parameters:
* IN http_method_t method ; HTTP method
*
* Description: A wrapper function that maps a method id to a method
* nameConverts a http_method id stored in the HTTP Method
*
* Returns:
* const char* ptr - Ptr to the HTTP Method
************************************************************************/
const char *method_to_str(http_method_t method);
#ifdef DEBUG
void print_http_headers(
http_message_t *hmsg);
#else
#define print_http_headers(hmsg) \
do { \
} while (0)
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
LinkedList.h
print_http_headers
void print_http_headers(http_message_t *hmsg)
Print the HTTP headers.
Definition httpparser.c:2223
parse_status_t
parse_status_t
Definition httpparser.h:152
PARSE_INCOMPLETE
@ PARSE_INCOMPLETE
Definition httpparser.h:156
PARSE_NO_MATCH
@ PARSE_NO_MATCH
Definition httpparser.h:164
PARSE_FAILURE
@ PARSE_FAILURE
Definition httpparser.h:160
PARSE_INCOMPLETE_ENTITY
@ PARSE_INCOMPLETE_ENTITY
Definition httpparser.h:158
PARSE_SUCCESS
@ PARSE_SUCCESS
Definition httpparser.h:154
PARSE_OK
@ PARSE_OK
Definition httpparser.h:162
PARSE_CONTINUE_1
@ PARSE_CONTINUE_1
Definition httpparser.h:166
membuffer.h
LINKEDLIST
Definition LinkedList.h:84
URI
Represents a URI used in parse_uri and elsewhere.
Definition uri.h:134
http_header_t
Definition httpparser.h:170
http_header_t::name
memptr name
Definition httpparser.h:172
http_header_t::value
membuffer value
Definition httpparser.h:176
http_header_t::name_id
int name_id
Definition httpparser.h:174
http_message_t
Definition httpparser.h:182
http_message_t::uri
uri_type uri
Definition httpparser.h:187
http_message_t::method
http_method_t method
Definition httpparser.h:185
http_message_t::is_request
int is_request
Definition httpparser.h:200
http_message_t::status_msg
membuffer status_msg
Definition httpparser.h:193
http_message_t::entity
memptr entity
Definition httpparser.h:208
http_message_t::urlbuf
char * urlbuf
Definition httpparser.h:213
http_message_t::amount_discarded
size_t amount_discarded
Definition httpparser.h:197
http_message_t::status_code
int status_code
Definition httpparser.h:191
http_message_t::msg
membuffer msg
Definition httpparser.h:211
http_message_t::request_method
http_method_t request_method
Definition httpparser.h:189
http_message_t::headers
LinkedList headers
Definition httpparser.h:206
http_parser_t
Definition httpparser.h:217
http_parser_t::http_error_code
int http_error_code
Definition httpparser.h:221
http_parser_t::valid_ssdp_notify_hack
int valid_ssdp_notify_hack
Definition httpparser.h:224
http_parser_t::entity_start_position
size_t entity_start_position
Definition httpparser.h:232
membuffer
Definition membuffer.h:58
memptr
Definition membuffer.h:48
scanner_t
Definition httpparser.h:61
scanner_t::entire_msg_loaded
int entire_msg_loaded
Definition httpparser.h:68
scanner_t::msg
membuffer * msg
Definition httpparser.h:63
scanner_t::cursor
size_t cursor
Definition httpparser.h:65
upnputil.h
uri.h