http

Committer:
reeml3
Date:
Mon Apr 15 18:12:24 2019 +0000
Revision:
1:ce6ccd14af4c
Parent:
0:a49e37a83a7a
http

Who changed what in which revision?

UserRevisionLine numberNew 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 _MBED_HTTP_REQUEST_BUILDER_H_
DuaaAbusharkh 0:a49e37a83a7a 19 #define _MBED_HTTP_REQUEST_BUILDER_H_
DuaaAbusharkh 0:a49e37a83a7a 20
DuaaAbusharkh 0:a49e37a83a7a 21 #include <string>
DuaaAbusharkh 0:a49e37a83a7a 22 #include <map>
DuaaAbusharkh 0:a49e37a83a7a 23 #include "http_parser.h"
DuaaAbusharkh 0:a49e37a83a7a 24 #include "http_parsed_url.h"
DuaaAbusharkh 0:a49e37a83a7a 25
DuaaAbusharkh 0:a49e37a83a7a 26 class HttpRequestBuilder {
DuaaAbusharkh 0:a49e37a83a7a 27 public:
DuaaAbusharkh 0:a49e37a83a7a 28 HttpRequestBuilder(http_method a_method, ParsedUrl* a_parsed_url)
DuaaAbusharkh 0:a49e37a83a7a 29 : method(a_method), parsed_url(a_parsed_url)
DuaaAbusharkh 0:a49e37a83a7a 30 {
DuaaAbusharkh 0:a49e37a83a7a 31 set_header("Host", string(parsed_url->host()));
DuaaAbusharkh 0:a49e37a83a7a 32 }
DuaaAbusharkh 0:a49e37a83a7a 33
DuaaAbusharkh 0:a49e37a83a7a 34 /**
DuaaAbusharkh 0:a49e37a83a7a 35 * Set a header for the request
DuaaAbusharkh 0:a49e37a83a7a 36 * If the key already exists, it will be overwritten...
DuaaAbusharkh 0:a49e37a83a7a 37 */
DuaaAbusharkh 0:a49e37a83a7a 38 void set_header(string key, string value) {
DuaaAbusharkh 0:a49e37a83a7a 39 map<string, string>::iterator it = headers.find(key);
DuaaAbusharkh 0:a49e37a83a7a 40
DuaaAbusharkh 0:a49e37a83a7a 41 if (it != headers.end()) {
DuaaAbusharkh 0:a49e37a83a7a 42 it->second = value;
DuaaAbusharkh 0:a49e37a83a7a 43 }
DuaaAbusharkh 0:a49e37a83a7a 44 else {
DuaaAbusharkh 0:a49e37a83a7a 45 headers.insert(headers.end(), pair<string, string>(key, value));
DuaaAbusharkh 0:a49e37a83a7a 46 }
DuaaAbusharkh 0:a49e37a83a7a 47 }
DuaaAbusharkh 0:a49e37a83a7a 48
DuaaAbusharkh 0:a49e37a83a7a 49 char* build(const void* body, size_t body_size, size_t &size) {
DuaaAbusharkh 0:a49e37a83a7a 50 const char* method_str = http_method_str(method);
DuaaAbusharkh 0:a49e37a83a7a 51
DuaaAbusharkh 0:a49e37a83a7a 52 if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0) {
DuaaAbusharkh 0:a49e37a83a7a 53 char buffer[10];
DuaaAbusharkh 0:a49e37a83a7a 54 snprintf(buffer, 10, "%d", body_size);
DuaaAbusharkh 0:a49e37a83a7a 55 set_header("Content-Length", string(buffer));
DuaaAbusharkh 0:a49e37a83a7a 56 }
DuaaAbusharkh 0:a49e37a83a7a 57
DuaaAbusharkh 0:a49e37a83a7a 58 size = 0;
DuaaAbusharkh 0:a49e37a83a7a 59
DuaaAbusharkh 0:a49e37a83a7a 60 // first line is METHOD PATH+QUERY HTTP/1.1\r\n
DuaaAbusharkh 0:a49e37a83a7a 61 size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
DuaaAbusharkh 0:a49e37a83a7a 62
DuaaAbusharkh 0:a49e37a83a7a 63 // after that we'll do the headers
DuaaAbusharkh 0:a49e37a83a7a 64 typedef map<string, string>::iterator it_type;
DuaaAbusharkh 0:a49e37a83a7a 65 for(it_type it = headers.begin(); it != headers.end(); it++) {
DuaaAbusharkh 0:a49e37a83a7a 66 // line is KEY: VALUE\r\n
DuaaAbusharkh 0:a49e37a83a7a 67 size += it->first.length() + 1 + 1 + it->second.length() + 2;
DuaaAbusharkh 0:a49e37a83a7a 68 }
DuaaAbusharkh 0:a49e37a83a7a 69
DuaaAbusharkh 0:a49e37a83a7a 70 // then the body, first an extra newline
DuaaAbusharkh 0:a49e37a83a7a 71 size += 2;
DuaaAbusharkh 0:a49e37a83a7a 72
DuaaAbusharkh 0:a49e37a83a7a 73 // body
DuaaAbusharkh 0:a49e37a83a7a 74 size += body_size;
DuaaAbusharkh 0:a49e37a83a7a 75
DuaaAbusharkh 0:a49e37a83a7a 76 // extra newline
DuaaAbusharkh 0:a49e37a83a7a 77 size += 2;
DuaaAbusharkh 0:a49e37a83a7a 78
DuaaAbusharkh 0:a49e37a83a7a 79 // Now let's print it
DuaaAbusharkh 0:a49e37a83a7a 80 char* req = (char*)calloc(size + 1, 1);
DuaaAbusharkh 0:a49e37a83a7a 81 char* originalReq = req;
DuaaAbusharkh 0:a49e37a83a7a 82
DuaaAbusharkh 0:a49e37a83a7a 83 if (strlen(parsed_url->query())) {
DuaaAbusharkh 0:a49e37a83a7a 84 sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
DuaaAbusharkh 0:a49e37a83a7a 85 } else {
DuaaAbusharkh 0:a49e37a83a7a 86 sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
DuaaAbusharkh 0:a49e37a83a7a 87 }
DuaaAbusharkh 0:a49e37a83a7a 88 req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2;
DuaaAbusharkh 0:a49e37a83a7a 89
DuaaAbusharkh 0:a49e37a83a7a 90 typedef map<string, string>::iterator it_type;
DuaaAbusharkh 0:a49e37a83a7a 91 for(it_type it = headers.begin(); it != headers.end(); it++) {
DuaaAbusharkh 0:a49e37a83a7a 92 // line is KEY: VALUE\r\n
DuaaAbusharkh 0:a49e37a83a7a 93 sprintf(req, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
DuaaAbusharkh 0:a49e37a83a7a 94 req += it->first.length() + 1 + 1 + it->second.length() + 2;
DuaaAbusharkh 0:a49e37a83a7a 95 }
DuaaAbusharkh 0:a49e37a83a7a 96
DuaaAbusharkh 0:a49e37a83a7a 97 sprintf(req, "\r\n");
DuaaAbusharkh 0:a49e37a83a7a 98 req += 2;
DuaaAbusharkh 0:a49e37a83a7a 99
DuaaAbusharkh 0:a49e37a83a7a 100 if (body_size > 0) {
DuaaAbusharkh 0:a49e37a83a7a 101 memcpy(req, body, body_size);
DuaaAbusharkh 0:a49e37a83a7a 102 }
DuaaAbusharkh 0:a49e37a83a7a 103 req += body_size;
DuaaAbusharkh 0:a49e37a83a7a 104
DuaaAbusharkh 0:a49e37a83a7a 105 sprintf(req, "\r\n");
DuaaAbusharkh 0:a49e37a83a7a 106 req += 2;
DuaaAbusharkh 0:a49e37a83a7a 107
DuaaAbusharkh 0:a49e37a83a7a 108 // Uncomment to debug...
DuaaAbusharkh 0:a49e37a83a7a 109 // printf("----- BEGIN REQUEST -----\n");
DuaaAbusharkh 0:a49e37a83a7a 110 // printf("%s", originalReq);
DuaaAbusharkh 0:a49e37a83a7a 111 // printf("----- END REQUEST -----\n");
DuaaAbusharkh 0:a49e37a83a7a 112
DuaaAbusharkh 0:a49e37a83a7a 113 return originalReq;
DuaaAbusharkh 0:a49e37a83a7a 114 }
DuaaAbusharkh 0:a49e37a83a7a 115
DuaaAbusharkh 0:a49e37a83a7a 116 private:
DuaaAbusharkh 0:a49e37a83a7a 117 http_method method;
DuaaAbusharkh 0:a49e37a83a7a 118 ParsedUrl* parsed_url;
DuaaAbusharkh 0:a49e37a83a7a 119 map<string, string> headers;
DuaaAbusharkh 0:a49e37a83a7a 120 };
DuaaAbusharkh 0:a49e37a83a7a 121
DuaaAbusharkh 0:a49e37a83a7a 122 #endif // _MBED_HTTP_REQUEST_BUILDER_H_