Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions include/net/http_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ typedef unsigned __int64 u64_t;
#include <zephyr/types.h>
#include <stddef.h>
#endif
#include <net/http_parser_state.h>
#include <net/http_parser_url.h>

/* Maximium header size allowed. If the macro is not defined
* before including this header then the default is used. To
Expand Down Expand Up @@ -229,38 +231,6 @@ struct http_parser_settings {
};


enum http_parser_url_fields {
UF_SCHEMA = 0
, UF_HOST = 1
, UF_PORT = 2
, UF_PATH = 3
, UF_QUERY = 4
, UF_FRAGMENT = 5
, UF_USERINFO = 6
, UF_MAX = 7
};


/* Result structure for http_parser_parse_url().
*
* Callers should index into field_data[] with UF_* values iff field_set
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
* because we probably have padding left over), we convert any port to
* a u16_t.
*/
struct http_parser_url {
u16_t field_set; /* Bitmask of (1 << UF_*) values */
u16_t port; /* Converted UF_PORT string */

struct {
u16_t off; /* Offset into buffer in which field
* starts
*/
u16_t len; /* Length of run in buffer */
} field_data[UF_MAX];
};


/* Returns the library version. Bits 16-23 contain the major version number,
* bits 8-15 the minor version number and bits 0-7 the patch level.
* Usage example:
Expand Down Expand Up @@ -306,13 +276,6 @@ const char *http_errno_name(enum http_errno err);
/* Return a string description of the given error */
const char *http_errno_description(enum http_errno err);

/* Initialize all http_parser_url members to 0 */
void http_parser_url_init(struct http_parser_url *u);

/* Parse a URL; return nonzero on failure */
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect, struct http_parser_url *u);

/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(struct http_parser *parser, int paused);

Expand Down
99 changes: 99 additions & 0 deletions include/net/http_parser_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef _HTTP_PARSER_STATE_H_
#define _HTTP_PARSER_STATE_H_
#ifdef __cplusplus
extern "C" {
#endif

enum state {
s_dead = 1, /* important that this is > 0 */
s_start_req_or_res,
s_res_or_resp_H,
s_start_res,
s_res_H,
s_res_HT,
s_res_HTT,
s_res_HTTP,
s_res_first_http_major,
s_res_http_major,
s_res_first_http_minor,
s_res_http_minor,
s_res_first_status_code,
s_res_status_code,
s_res_status_start,
s_res_status,
s_res_line_almost_done,
s_start_req,
s_req_method,
s_req_spaces_before_url,
s_req_schema,
s_req_schema_slash,
s_req_schema_slash_slash,
s_req_server_start,
s_req_server,
s_req_server_with_at,
s_req_path,
s_req_query_string_start,
s_req_query_string,
s_req_fragment_start,
s_req_fragment,
s_req_http_start,
s_req_http_H,
s_req_http_HT,
s_req_http_HTT,
s_req_http_HTTP,
s_req_first_http_major,
s_req_http_major,
s_req_first_http_minor,
s_req_http_minor,
s_req_line_almost_done,
s_header_field_start,
s_header_field,
s_header_value_discard_ws,
s_header_value_discard_ws_almost_done,
s_header_value_discard_lws,
s_header_value_start,
s_header_value,
s_header_value_lws,
s_header_almost_done,
s_chunk_size_start,
s_chunk_size,
s_chunk_parameters,
s_chunk_size_almost_done,
s_headers_almost_done,
s_headers_done,
/* Important: 's_headers_done' must be the last 'header' state. All
* states beyond this must be 'body' states. It is used for overflow
* checking. See the PARSING_HEADER() macro.
*/
s_chunk_data,
s_chunk_data_almost_done,
s_chunk_data_done,
s_body_identity,
s_body_identity_eof,
s_message_done
};

#ifdef __cplusplus
}
#endif
#endif
74 changes: 74 additions & 0 deletions include/net/http_parser_url.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef _HTTP_PARSER_URL_H_
#define _HTTP_PARSER_URL_H_
#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <zephyr/types.h>
#include <stddef.h>
#include <net/http_parser_state.h>

enum http_parser_url_fields {
UF_SCHEMA = 0
, UF_HOST = 1
, UF_PORT = 2
, UF_PATH = 3
, UF_QUERY = 4
, UF_FRAGMENT = 5
, UF_USERINFO = 6
, UF_MAX = 7
};

/* Result structure for http_parser_url_fields().
*
* Callers should index into field_data[] with UF_* values iff field_set
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
* because we probably have padding left over), we convert any port to
* a u16_t.
*/
struct http_parser_url {
u16_t field_set; /* Bitmask of (1 << UF_*) values */
u16_t port; /* Converted UF_PORT string */

struct {
u16_t off; /* Offset into buffer in which field
* starts
*/
u16_t len; /* Length of run in buffer */
} field_data[UF_MAX];
};

enum state parse_url_char(enum state s, const char ch);

/* Initialize all http_parser_url members to 0 */
void http_parser_url_init(struct http_parser_url *u);

/* Parse a URL; return nonzero on failure */
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect, struct http_parser_url *u);

#ifdef __cplusplus
}
#endif
#endif
12 changes: 11 additions & 1 deletion subsys/net/lib/http/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,25 @@ config HTTP_PARSER
bool "HTTP Parser support"
default n
select HTTP
select HTTP_PARSER_URL
help
This option enables the http_parser library from nodejs.
This parser requires some string-related routines commonly
provided by a libc implementation.

config HTTP_PARSER_URL
bool "HTTP Parser for URL support"
default n
select HTTP
help
This option enables the URI parser library based on source from nodejs.
This parser requires some string-related routines commonly
provided by a libc implementation.

config HTTP_PARSER_STRICT
bool "HTTP strict parsing"
default n
depends on HTTP_PARSER
depends on (HTTP_PARSER || HTTP_PARSER_URL)
help
This option enables the strict parsing option

Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/http/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ccflags-$(CONFIG_HTTP_PARSER_STRICT) += -DHTTP_PARSER_STRICT

obj-$(CONFIG_HTTP_PARSER) := http_parser.o
obj-$(CONFIG_HTTP_PARSER_URL) += http_parser_url.o
obj-$(CONFIG_HTTP_CLIENT) += http_client.o
obj-$(CONFIG_HTTP_SERVER) += http_server.o
3 changes: 3 additions & 0 deletions subsys/net/lib/http/README_http_parser
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ can be found at:
https://github.com/nodejs/http-parser/releases/tag/v2.7.1
https://github.com/nodejs/http-parser/archive/v2.7.1.tar.gz

NOTE: The portions which relate to URL parsing have been split out into
http_parser_url.c (orignially located in http_parser.c).

* "http-parser" is the project's name, "http_parser" is used in filenames.
Loading