Reem M
/
http-server-example1
http
mbed-http/source/http_request_parser.h@0:a49e37a83a7a, 2019-04-15 (annotated)
- Committer:
- DuaaAbusharkh
- Date:
- Mon Apr 15 08:30:22 2019 +0000
- Revision:
- 0:a49e37a83a7a
blink
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuaaAbusharkh | 0:a49e37a83a7a | 1 | /* |
DuaaAbusharkh | 0:a49e37a83a7a | 2 | * PackageLicenseDeclared: Apache-2.0 |
DuaaAbusharkh | 0:a49e37a83a7a | 3 | * Copyright (c) 2017 ARM Limited |
DuaaAbusharkh | 0:a49e37a83a7a | 4 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DuaaAbusharkh | 0:a49e37a83a7a | 6 | * you may not use this file except in compliance with the License. |
DuaaAbusharkh | 0:a49e37a83a7a | 7 | * You may obtain a copy of the License at |
DuaaAbusharkh | 0:a49e37a83a7a | 8 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
DuaaAbusharkh | 0:a49e37a83a7a | 10 | * |
DuaaAbusharkh | 0:a49e37a83a7a | 11 | * Unless required by applicable law or agreed to in writing, software |
DuaaAbusharkh | 0:a49e37a83a7a | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
DuaaAbusharkh | 0:a49e37a83a7a | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DuaaAbusharkh | 0:a49e37a83a7a | 14 | * See the License for the specific language governing permissions and |
DuaaAbusharkh | 0:a49e37a83a7a | 15 | * limitations under the License. |
DuaaAbusharkh | 0:a49e37a83a7a | 16 | */ |
DuaaAbusharkh | 0:a49e37a83a7a | 17 | |
DuaaAbusharkh | 0:a49e37a83a7a | 18 | #ifndef _HTTP_RESPONSE_PARSER_H_ |
DuaaAbusharkh | 0:a49e37a83a7a | 19 | #define _HTTP_RESPONSE_PARSER_H_ |
DuaaAbusharkh | 0:a49e37a83a7a | 20 | |
DuaaAbusharkh | 0:a49e37a83a7a | 21 | #include "http_parser.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 22 | #include "http_response.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 23 | |
DuaaAbusharkh | 0:a49e37a83a7a | 24 | class HttpParser { |
DuaaAbusharkh | 0:a49e37a83a7a | 25 | public: |
DuaaAbusharkh | 0:a49e37a83a7a | 26 | |
DuaaAbusharkh | 0:a49e37a83a7a | 27 | HttpParser(HttpResponse* a_response, http_parser_type parser_type, Callback<void(const char *at, size_t length)> a_body_callback = 0) |
DuaaAbusharkh | 0:a49e37a83a7a | 28 | : response(a_response), body_callback(a_body_callback) |
DuaaAbusharkh | 0:a49e37a83a7a | 29 | { |
DuaaAbusharkh | 0:a49e37a83a7a | 30 | settings = new http_parser_settings(); |
DuaaAbusharkh | 0:a49e37a83a7a | 31 | |
DuaaAbusharkh | 0:a49e37a83a7a | 32 | settings->on_message_begin = &HttpParser::on_message_begin_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 33 | settings->on_url = &HttpParser::on_url_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 34 | settings->on_status = &HttpParser::on_status_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 35 | settings->on_header_field = &HttpParser::on_header_field_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 36 | settings->on_header_value = &HttpParser::on_header_value_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 37 | settings->on_headers_complete = &HttpParser::on_headers_complete_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 38 | settings->on_chunk_header = &HttpParser::on_chunk_header_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 39 | settings->on_chunk_complete = &HttpParser::on_chunk_complete_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 40 | settings->on_body = &HttpParser::on_body_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 41 | settings->on_message_complete = &HttpParser::on_message_complete_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 42 | |
DuaaAbusharkh | 0:a49e37a83a7a | 43 | // Construct the http_parser object |
DuaaAbusharkh | 0:a49e37a83a7a | 44 | parser = new http_parser(); |
DuaaAbusharkh | 0:a49e37a83a7a | 45 | http_parser_init(parser, parser_type); |
DuaaAbusharkh | 0:a49e37a83a7a | 46 | parser->data = (void*)this; |
DuaaAbusharkh | 0:a49e37a83a7a | 47 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 48 | |
DuaaAbusharkh | 0:a49e37a83a7a | 49 | ~HttpParser() { |
DuaaAbusharkh | 0:a49e37a83a7a | 50 | if (parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 51 | delete parser; |
DuaaAbusharkh | 0:a49e37a83a7a | 52 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 53 | if (settings) { |
DuaaAbusharkh | 0:a49e37a83a7a | 54 | delete settings; |
DuaaAbusharkh | 0:a49e37a83a7a | 55 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 56 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 57 | |
DuaaAbusharkh | 0:a49e37a83a7a | 58 | size_t execute(const char* buffer, size_t buffer_size) { |
DuaaAbusharkh | 0:a49e37a83a7a | 59 | return http_parser_execute(parser, settings, buffer, buffer_size); |
DuaaAbusharkh | 0:a49e37a83a7a | 60 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 61 | |
DuaaAbusharkh | 0:a49e37a83a7a | 62 | void finish() { |
DuaaAbusharkh | 0:a49e37a83a7a | 63 | http_parser_execute(parser, settings, NULL, 0); |
DuaaAbusharkh | 0:a49e37a83a7a | 64 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 65 | |
DuaaAbusharkh | 0:a49e37a83a7a | 66 | private: |
DuaaAbusharkh | 0:a49e37a83a7a | 67 | // Member functions |
DuaaAbusharkh | 0:a49e37a83a7a | 68 | int on_message_begin(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 69 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 70 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 71 | |
DuaaAbusharkh | 0:a49e37a83a7a | 72 | int on_url(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 73 | string s(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 74 | response->set_url(s); |
DuaaAbusharkh | 0:a49e37a83a7a | 75 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 76 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 77 | |
DuaaAbusharkh | 0:a49e37a83a7a | 78 | int on_status(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 79 | string s(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 80 | response->set_status(parser->status_code, s); |
DuaaAbusharkh | 0:a49e37a83a7a | 81 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 82 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 83 | |
DuaaAbusharkh | 0:a49e37a83a7a | 84 | int on_header_field(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 85 | string s(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 86 | response->set_header_field(s); |
DuaaAbusharkh | 0:a49e37a83a7a | 87 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 88 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 89 | |
DuaaAbusharkh | 0:a49e37a83a7a | 90 | int on_header_value(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 91 | string s(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 92 | response->set_header_value(s); |
DuaaAbusharkh | 0:a49e37a83a7a | 93 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 94 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 95 | |
DuaaAbusharkh | 0:a49e37a83a7a | 96 | int on_headers_complete(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 97 | response->set_headers_complete(); |
DuaaAbusharkh | 0:a49e37a83a7a | 98 | response->set_method((http_method)parser->method); |
DuaaAbusharkh | 0:a49e37a83a7a | 99 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 100 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 101 | |
DuaaAbusharkh | 0:a49e37a83a7a | 102 | int on_body(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 103 | response->increase_body_length(length); |
DuaaAbusharkh | 0:a49e37a83a7a | 104 | |
DuaaAbusharkh | 0:a49e37a83a7a | 105 | if (body_callback) { |
DuaaAbusharkh | 0:a49e37a83a7a | 106 | body_callback(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 107 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 108 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 109 | |
DuaaAbusharkh | 0:a49e37a83a7a | 110 | response->set_body(at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 111 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 112 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 113 | |
DuaaAbusharkh | 0:a49e37a83a7a | 114 | int on_message_complete(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 115 | response->set_message_complete(); |
DuaaAbusharkh | 0:a49e37a83a7a | 116 | |
DuaaAbusharkh | 0:a49e37a83a7a | 117 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 118 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 119 | |
DuaaAbusharkh | 0:a49e37a83a7a | 120 | int on_chunk_header(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 121 | response->set_chunked(); |
DuaaAbusharkh | 0:a49e37a83a7a | 122 | |
DuaaAbusharkh | 0:a49e37a83a7a | 123 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 124 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 125 | |
DuaaAbusharkh | 0:a49e37a83a7a | 126 | int on_chunk_complete(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 127 | return 0; |
DuaaAbusharkh | 0:a49e37a83a7a | 128 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 129 | |
DuaaAbusharkh | 0:a49e37a83a7a | 130 | // Static http_parser callback functions |
DuaaAbusharkh | 0:a49e37a83a7a | 131 | static int on_message_begin_callback(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 132 | return ((HttpParser*)parser->data)->on_message_begin(parser); |
DuaaAbusharkh | 0:a49e37a83a7a | 133 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 134 | |
DuaaAbusharkh | 0:a49e37a83a7a | 135 | static int on_url_callback(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 136 | return ((HttpParser*)parser->data)->on_url(parser, at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 137 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 138 | |
DuaaAbusharkh | 0:a49e37a83a7a | 139 | static int on_status_callback(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 140 | return ((HttpParser*)parser->data)->on_status(parser, at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 141 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 142 | |
DuaaAbusharkh | 0:a49e37a83a7a | 143 | static int on_header_field_callback(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 144 | return ((HttpParser*)parser->data)->on_header_field(parser, at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 145 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 146 | |
DuaaAbusharkh | 0:a49e37a83a7a | 147 | static int on_header_value_callback(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 148 | return ((HttpParser*)parser->data)->on_header_value(parser, at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 149 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 150 | |
DuaaAbusharkh | 0:a49e37a83a7a | 151 | static int on_headers_complete_callback(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 152 | return ((HttpParser*)parser->data)->on_headers_complete(parser); |
DuaaAbusharkh | 0:a49e37a83a7a | 153 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 154 | |
DuaaAbusharkh | 0:a49e37a83a7a | 155 | static int on_body_callback(http_parser* parser, const char *at, size_t length) { |
DuaaAbusharkh | 0:a49e37a83a7a | 156 | return ((HttpParser*)parser->data)->on_body(parser, at, length); |
DuaaAbusharkh | 0:a49e37a83a7a | 157 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 158 | |
DuaaAbusharkh | 0:a49e37a83a7a | 159 | static int on_message_complete_callback(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 160 | return ((HttpParser*)parser->data)->on_message_complete(parser); |
DuaaAbusharkh | 0:a49e37a83a7a | 161 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 162 | |
DuaaAbusharkh | 0:a49e37a83a7a | 163 | static int on_chunk_header_callback(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 164 | return ((HttpParser*)parser->data)->on_chunk_header(parser); |
DuaaAbusharkh | 0:a49e37a83a7a | 165 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 166 | |
DuaaAbusharkh | 0:a49e37a83a7a | 167 | static int on_chunk_complete_callback(http_parser* parser) { |
DuaaAbusharkh | 0:a49e37a83a7a | 168 | return ((HttpParser*)parser->data)->on_chunk_complete(parser); |
DuaaAbusharkh | 0:a49e37a83a7a | 169 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 170 | |
DuaaAbusharkh | 0:a49e37a83a7a | 171 | HttpResponse* response; |
DuaaAbusharkh | 0:a49e37a83a7a | 172 | Callback<void(const char *at, size_t length)> body_callback; |
DuaaAbusharkh | 0:a49e37a83a7a | 173 | http_parser* parser; |
DuaaAbusharkh | 0:a49e37a83a7a | 174 | http_parser_settings* settings; |
DuaaAbusharkh | 0:a49e37a83a7a | 175 | }; |
DuaaAbusharkh | 0:a49e37a83a7a | 176 | |
DuaaAbusharkh | 0:a49e37a83a7a | 177 | #endif // _HTTP_RESPONSE_PARSER_H_ |