Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-http by
http_response.h
00001 /* 00002 * PackageLicenseDeclared: Apache-2.0 00003 * Copyright (c) 2017 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef _MBED_HTTP_HTTP_RESPONSE 00019 #define _MBED_HTTP_HTTP_RESPONSE 00020 #include <string> 00021 #include <vector> 00022 #include "http_parser.h" 00023 00024 using namespace std; 00025 00026 class HttpResponse { 00027 public: 00028 HttpResponse() { 00029 status_code = 0; 00030 concat_header_field = false; 00031 concat_header_value = false; 00032 expected_content_length = 0; 00033 is_chunked = false; 00034 is_message_completed = false; 00035 body_length = 0; 00036 body_offset = 0; 00037 body = NULL; 00038 } 00039 00040 ~HttpResponse() { 00041 if (body != NULL) { 00042 free(body); 00043 } 00044 00045 for (size_t ix = 0; ix < header_fields.size(); ix++) { 00046 delete header_fields[ix]; 00047 delete header_values[ix]; 00048 } 00049 } 00050 00051 void set_status(int a_status_code, string a_status_message) { 00052 status_code = a_status_code; 00053 status_message = a_status_message; 00054 } 00055 00056 int get_status_code() { 00057 return status_code; 00058 } 00059 00060 string get_status_message() { 00061 return status_message; 00062 } 00063 00064 void set_header_field(string field) { 00065 concat_header_value = false; 00066 00067 // headers can be chunked 00068 if (concat_header_field) { 00069 *header_fields[header_fields.size() - 1] = (*header_fields[header_fields.size() - 1]) + field; 00070 } 00071 else { 00072 header_fields.push_back(new string(field)); 00073 } 00074 00075 concat_header_field = true; 00076 } 00077 00078 void set_header_value(string value) { 00079 concat_header_field = false; 00080 00081 // headers can be chunked 00082 if (concat_header_value) { 00083 *header_values[header_values.size() - 1] = (*header_values[header_values.size() - 1]) + value; 00084 } 00085 else { 00086 header_values.push_back(new string(value)); 00087 } 00088 00089 concat_header_value = true; 00090 } 00091 00092 void set_headers_complete() { 00093 for (size_t ix = 0; ix < header_fields.size(); ix++) { 00094 if (strcicmp(header_fields[ix]->c_str(), "content-length") == 0) { 00095 expected_content_length = (size_t)atoi(header_values[ix]->c_str()); 00096 break; 00097 } 00098 } 00099 } 00100 00101 size_t get_headers_length() { 00102 return header_fields.size(); 00103 } 00104 00105 vector<string*> get_headers_fields() { 00106 return header_fields; 00107 } 00108 00109 vector<string*> get_headers_values() { 00110 return header_values; 00111 } 00112 00113 void set_body(const char *at, size_t length) { 00114 // only malloc when this fn is called, so we don't alloc when body callback's are enabled 00115 if (body == NULL && !is_chunked) { 00116 body = (char*)malloc(expected_content_length); 00117 } 00118 00119 if (is_chunked) { 00120 if (body == NULL) { 00121 body = (char*)malloc(length); 00122 } 00123 else { 00124 body = (char*)realloc(body, body_offset + length); 00125 } 00126 } 00127 00128 memcpy(body + body_offset, at, length); 00129 00130 body_offset += length; 00131 } 00132 00133 void* get_body() { 00134 return (void*)body; 00135 } 00136 00137 string get_body_as_string() { 00138 string s(body, body_offset); 00139 return s; 00140 } 00141 00142 void increase_body_length(size_t length) { 00143 body_length += length; 00144 } 00145 00146 size_t get_body_length() { 00147 return body_offset; 00148 } 00149 00150 bool is_message_complete() { 00151 return is_message_completed; 00152 } 00153 00154 void set_chunked() { 00155 is_chunked = true; 00156 } 00157 00158 void set_message_complete() { 00159 is_message_completed = true; 00160 } 00161 00162 private: 00163 // from http://stackoverflow.com/questions/5820810/case-insensitive-string-comp-in-c 00164 int strcicmp(char const *a, char const *b) { 00165 for (;; a++, b++) { 00166 int d = tolower(*a) - tolower(*b); 00167 if (d != 0 || !*a) { 00168 return d; 00169 } 00170 } 00171 } 00172 00173 char tolower(char c) { 00174 if(('A' <= c) && (c <= 'Z')) { 00175 return 'a' + (c - 'A'); 00176 } 00177 00178 return c; 00179 } 00180 00181 int status_code; 00182 string status_message; 00183 00184 vector<string*> header_fields; 00185 vector<string*> header_values; 00186 00187 bool concat_header_field; 00188 bool concat_header_value; 00189 00190 size_t expected_content_length; 00191 00192 bool is_chunked; 00193 00194 bool is_message_completed; 00195 00196 char * body; 00197 size_t body_length; 00198 size_t body_offset; 00199 }; 00200 #endif
Generated on Fri Jul 22 2022 08:07:18 by
1.7.2
