mbed-http
Fork of mbed-http by
Revision 15:ffc77f212382, committed 2017-07-28
- Comitter:
- Jan Jongboom
- Date:
- Fri Jul 28 14:26:26 2017 +0200
- Parent:
- 14:3004056e4661
- Child:
- 16:1c918cc026c5
- Commit message:
- Add request parsing to prepare for HTTP server example
Changed in this revision
--- a/source/http_request.h Thu Mar 30 15:13:37 2017 +0200
+++ b/source/http_request.h Fri Jul 28 14:26:26 2017 +0200
@@ -24,7 +24,7 @@
#include "http_parser.h"
#include "http_response.h"
#include "http_request_builder.h"
-#include "http_response_parser.h"
+#include "http_request_parser.h"
#include "http_parsed_url.h"
/**
@@ -148,7 +148,7 @@
// Create a response object
response = new HttpResponse();
// And a response parser
- HttpResponseParser parser(response, body_callback);
+ HttpParser parser(response, HTTP_RESPONSE, body_callback);
// Set up a receive buffer (on the heap)
uint8_t* recv_buffer = (uint8_t*)malloc(HTTP_RECEIVE_BUFFER_SIZE);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/http_request_parser.h Fri Jul 28 14:26:26 2017 +0200
@@ -0,0 +1,177 @@
+/*
+ * PackageLicenseDeclared: Apache-2.0
+ * Copyright (c) 2017 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _HTTP_RESPONSE_PARSER_H_
+#define _HTTP_RESPONSE_PARSER_H_
+
+#include "http_parser.h"
+#include "http_response.h"
+
+class HttpParser {
+public:
+
+ HttpParser(HttpResponse* a_response, http_parser_type parser_type, Callback<void(const char *at, size_t length)> a_body_callback = 0)
+ : response(a_response), body_callback(a_body_callback)
+ {
+ settings = new http_parser_settings();
+
+ settings->on_message_begin = &HttpParser::on_message_begin_callback;
+ settings->on_url = &HttpParser::on_url_callback;
+ settings->on_status = &HttpParser::on_status_callback;
+ settings->on_header_field = &HttpParser::on_header_field_callback;
+ settings->on_header_value = &HttpParser::on_header_value_callback;
+ settings->on_headers_complete = &HttpParser::on_headers_complete_callback;
+ settings->on_chunk_header = &HttpParser::on_chunk_header_callback;
+ settings->on_chunk_complete = &HttpParser::on_chunk_complete_callback;
+ settings->on_body = &HttpParser::on_body_callback;
+ settings->on_message_complete = &HttpParser::on_message_complete_callback;
+
+ // Construct the http_parser object
+ parser = new http_parser();
+ http_parser_init(parser, parser_type);
+ parser->data = (void*)this;
+ }
+
+ ~HttpParser() {
+ if (parser) {
+ delete parser;
+ }
+ if (settings) {
+ delete settings;
+ }
+ }
+
+ size_t execute(const char* buffer, size_t buffer_size) {
+ return http_parser_execute(parser, settings, buffer, buffer_size);
+ }
+
+ void finish() {
+ http_parser_execute(parser, settings, NULL, 0);
+ }
+
+private:
+ // Member functions
+ int on_message_begin(http_parser* parser) {
+ return 0;
+ }
+
+ int on_url(http_parser* parser, const char *at, size_t length) {
+ string s(at, length);
+ response->set_url(s);
+ return 0;
+ }
+
+ int on_status(http_parser* parser, const char *at, size_t length) {
+ string s(at, length);
+ response->set_status(parser->status_code, s);
+ return 0;
+ }
+
+ int on_header_field(http_parser* parser, const char *at, size_t length) {
+ string s(at, length);
+ response->set_header_field(s);
+ return 0;
+ }
+
+ int on_header_value(http_parser* parser, const char *at, size_t length) {
+ string s(at, length);
+ response->set_header_value(s);
+ return 0;
+ }
+
+ int on_headers_complete(http_parser* parser) {
+ response->set_headers_complete();
+ response->set_method((http_method)parser->method);
+ return 0;
+ }
+
+ int on_body(http_parser* parser, const char *at, size_t length) {
+ response->increase_body_length(length);
+
+ if (body_callback) {
+ body_callback(at, length);
+ return 0;
+ }
+
+ response->set_body(at, length);
+ return 0;
+ }
+
+ int on_message_complete(http_parser* parser) {
+ response->set_message_complete();
+
+ return 0;
+ }
+
+ int on_chunk_header(http_parser* parser) {
+ response->set_chunked();
+
+ return 0;
+ }
+
+ int on_chunk_complete(http_parser* parser) {
+ return 0;
+ }
+
+ // Static http_parser callback functions
+ static int on_message_begin_callback(http_parser* parser) {
+ return ((HttpParser*)parser->data)->on_message_begin(parser);
+ }
+
+ static int on_url_callback(http_parser* parser, const char *at, size_t length) {
+ return ((HttpParser*)parser->data)->on_url(parser, at, length);
+ }
+
+ static int on_status_callback(http_parser* parser, const char *at, size_t length) {
+ return ((HttpParser*)parser->data)->on_status(parser, at, length);
+ }
+
+ static int on_header_field_callback(http_parser* parser, const char *at, size_t length) {
+ return ((HttpParser*)parser->data)->on_header_field(parser, at, length);
+ }
+
+ static int on_header_value_callback(http_parser* parser, const char *at, size_t length) {
+ return ((HttpParser*)parser->data)->on_header_value(parser, at, length);
+ }
+
+ static int on_headers_complete_callback(http_parser* parser) {
+ return ((HttpParser*)parser->data)->on_headers_complete(parser);
+ }
+
+ static int on_body_callback(http_parser* parser, const char *at, size_t length) {
+ return ((HttpParser*)parser->data)->on_body(parser, at, length);
+ }
+
+ static int on_message_complete_callback(http_parser* parser) {
+ return ((HttpParser*)parser->data)->on_message_complete(parser);
+ }
+
+ static int on_chunk_header_callback(http_parser* parser) {
+ return ((HttpParser*)parser->data)->on_chunk_header(parser);
+ }
+
+ static int on_chunk_complete_callback(http_parser* parser) {
+ return ((HttpParser*)parser->data)->on_chunk_complete(parser);
+ }
+
+ HttpResponse* response;
+ Callback<void(const char *at, size_t length)> body_callback;
+ http_parser* parser;
+ http_parser_settings* settings;
+};
+
+#endif // _HTTP_RESPONSE_PARSER_H_
--- a/source/http_response.h Thu Mar 30 15:13:37 2017 +0200
+++ b/source/http_response.h Fri Jul 28 14:26:26 2017 +0200
@@ -61,6 +61,22 @@
return status_message;
}
+ void set_url(string a_url) {
+ url = a_url;
+ }
+
+ string get_url() {
+ return url;
+ }
+
+ void set_method(http_method a_method) {
+ method = a_method;
+ }
+
+ http_method get_method() {
+ return method;
+ }
+
void set_header_field(string field) {
concat_header_value = false;
@@ -185,6 +201,8 @@
int status_code;
string status_message;
+ string url;
+ http_method method;
vector<string*> header_fields;
vector<string*> header_values;
@@ -202,4 +220,5 @@
size_t body_length;
size_t body_offset;
};
+
#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/http_server.h Fri Jul 28 14:26:26 2017 +0200 @@ -0,0 +1,23 @@ +/* + * PackageLicenseDeclared: Apache-2.0 + * Copyright (c) 2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _HTTP_SERVER_ +#define _HTTP_SERVER_ + + + +#endif // _HTTP_SERVER
--- a/source/https_request.h Thu Mar 30 15:13:37 2017 +0200
+++ b/source/https_request.h Fri Jul 28 14:26:26 2017 +0200
@@ -24,7 +24,7 @@
#include "http_parser.h"
#include "http_response.h"
#include "http_request_builder.h"
-#include "http_response_parser.h"
+#include "http_request_parser.h"
#include "http_parsed_url.h"
#include "tls_socket.h"
@@ -157,7 +157,7 @@
// Create a response object
_response = new HttpResponse();
// And a response parser
- HttpResponseParser parser(_response, _body_callback);
+ HttpParser parser(_response, HTTP_RESPONSE, _body_callback);
// Set up a receive buffer (on the heap)
uint8_t* recv_buffer = (uint8_t*)malloc(HTTP_RECEIVE_BUFFER_SIZE);
--- a/source/tls_socket.h Thu Mar 30 15:13:37 2017 +0200 +++ b/source/tls_socket.h Fri Jul 28 14:26:26 2017 +0200 @@ -27,7 +27,7 @@ #include "http_parser.h" #include "http_response.h" #include "http_request_builder.h" -#include "http_response_parser.h" +#include "http_request_parser.h" #include "http_parsed_url.h" #include "mbedtls/platform.h"
