|
| 1 | +#include "Client.h" |
| 2 | +#include "RestClient.h" |
| 3 | + |
| 4 | +#ifdef HTTP_DEBUG |
| 5 | +#define HTTP_DEBUG_PRINT(string) (Serial.print(string)) |
| 6 | +#endif |
| 7 | + |
| 8 | +#ifndef HTTP_DEBUG |
| 9 | +#define HTTP_DEBUG_PRINT(string) |
| 10 | +#endif |
| 11 | + |
| 12 | + |
| 13 | +RestClient::RestClient(Client& netClient, const char* _host) { |
| 14 | + host = _host; |
| 15 | + port = 80; |
| 16 | + num_headers = 0; |
| 17 | + contentType = "x-www-form-urlencoded"; // default |
| 18 | + this->client = &netClient; |
| 19 | +} |
| 20 | + |
| 21 | +RestClient::RestClient(Client& netClient, const char* _host, int _port) { |
| 22 | + host = _host; |
| 23 | + port = _port; |
| 24 | + num_headers = 0; |
| 25 | + contentType = "x-www-form-urlencoded"; // default |
| 26 | + this->client = &netClient; |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +// GET path |
| 33 | +int RestClient::get(const char* path){ |
| 34 | + return request("GET", path, NULL, NULL); |
| 35 | +} |
| 36 | + |
| 37 | +//GET path with response |
| 38 | +int RestClient::get(const char* path, String* response){ |
| 39 | + return request("GET", path, NULL, response); |
| 40 | +} |
| 41 | + |
| 42 | +// POST path and body |
| 43 | +int RestClient::post(const char* path, const char* body){ |
| 44 | + return request("POST", path, body, NULL); |
| 45 | +} |
| 46 | + |
| 47 | +// POST path and body with response |
| 48 | +int RestClient::post(const char* path, const char* body, String* response){ |
| 49 | + return request("POST", path, body, response); |
| 50 | +} |
| 51 | + |
| 52 | +// PUT path and body |
| 53 | +int RestClient::put(const char* path, const char* body){ |
| 54 | + return request("PUT", path, body, NULL); |
| 55 | +} |
| 56 | + |
| 57 | +// PUT path and body with response |
| 58 | +int RestClient::put(const char* path, const char* body, String* response){ |
| 59 | + return request("PUT", path, body, response); |
| 60 | +} |
| 61 | + |
| 62 | +// DELETE path |
| 63 | +int RestClient::del(const char* path){ |
| 64 | + return request("DELETE", path, NULL, NULL); |
| 65 | +} |
| 66 | + |
| 67 | +// DELETE path and response |
| 68 | +int RestClient::del(const char* path, String* response){ |
| 69 | + return request("DELETE", path, NULL, response); |
| 70 | +} |
| 71 | + |
| 72 | +// DELETE path and body |
| 73 | +int RestClient::del(const char* path, const char* body ){ |
| 74 | + return request("DELETE", path, body, NULL); |
| 75 | +} |
| 76 | + |
| 77 | +// DELETE path and body with response |
| 78 | +int RestClient::del(const char* path, const char* body, String* response){ |
| 79 | + return request("DELETE", path, body, response); |
| 80 | +} |
| 81 | + |
| 82 | +void RestClient::write(const char* string){ |
| 83 | + HTTP_DEBUG_PRINT(string); |
| 84 | + client->print(string); |
| 85 | +} |
| 86 | + |
| 87 | +void RestClient::setHeader(const char* header){ |
| 88 | + headers[num_headers] = header; |
| 89 | + num_headers++; |
| 90 | +} |
| 91 | + |
| 92 | +void RestClient::setContentType(const char* contentTypeValue){ |
| 93 | + contentType = contentTypeValue; |
| 94 | +} |
| 95 | + |
| 96 | +// The mother- generic request method. |
| 97 | +// |
| 98 | +int RestClient::request(const char* method, const char* path, |
| 99 | + const char* body, String* response){ |
| 100 | + |
| 101 | + HTTP_DEBUG_PRINT("HTTP: connect\n"); |
| 102 | + |
| 103 | + if(client->connect(host, port)){ |
| 104 | + HTTP_DEBUG_PRINT("HTTP: connected\n"); |
| 105 | + HTTP_DEBUG_PRINT("REQUEST: \n"); |
| 106 | + // Make a HTTP request line: |
| 107 | + write(method); |
| 108 | + write(" "); |
| 109 | + write(path); |
| 110 | + write(" HTTP/1.1\r\n"); |
| 111 | + for(int i=0; i<num_headers; i++){ |
| 112 | + write(headers[i]); |
| 113 | + write("\r\n"); |
| 114 | + } |
| 115 | + write("Host: "); |
| 116 | + write(host); |
| 117 | + write("\r\n"); |
| 118 | + write("Connection: close\r\n"); |
| 119 | + |
| 120 | + if(body != NULL){ |
| 121 | + char contentLength[30]; |
| 122 | + sprintf(contentLength, "Content-Length: %d\r\n", strlen(body)); |
| 123 | + write(contentLength); |
| 124 | + |
| 125 | + write("Content-Type: "); |
| 126 | + write(contentType); |
| 127 | + write("\r\n"); |
| 128 | + } |
| 129 | + |
| 130 | + write("\r\n"); |
| 131 | + |
| 132 | + if(body != NULL){ |
| 133 | + write(body); |
| 134 | + write("\r\n"); |
| 135 | + write("\r\n"); |
| 136 | + } |
| 137 | + |
| 138 | + //make sure you write all those bytes. |
| 139 | + delay(100); |
| 140 | + |
| 141 | + HTTP_DEBUG_PRINT("HTTP: call readResponse\n"); |
| 142 | + int statusCode = readResponse(response); |
| 143 | + HTTP_DEBUG_PRINT("HTTP: return readResponse\n"); |
| 144 | + |
| 145 | + //cleanup |
| 146 | + HTTP_DEBUG_PRINT("HTTP: stop client\n"); |
| 147 | + num_headers = 0; |
| 148 | + client->stop(); |
| 149 | + delay(50); |
| 150 | + HTTP_DEBUG_PRINT("HTTP: client stopped\n"); |
| 151 | + |
| 152 | + return statusCode; |
| 153 | + }else{ |
| 154 | + HTTP_DEBUG_PRINT("HTTP Connection failed\n"); |
| 155 | + return 0; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +int RestClient::readResponse(String* response) { |
| 160 | + |
| 161 | + // an http request ends with a blank line |
| 162 | + boolean currentLineIsBlank = true; |
| 163 | + boolean httpBody = false; |
| 164 | + boolean inStatus = false; |
| 165 | + |
| 166 | + char statusCode[4]; |
| 167 | + int i = 0; |
| 168 | + int code = 0; |
| 169 | + |
| 170 | + if(response == NULL){ |
| 171 | + HTTP_DEBUG_PRINT("HTTP: NULL RESPONSE POINTER: \n"); |
| 172 | + }else{ |
| 173 | + HTTP_DEBUG_PRINT("HTTP: NON-NULL RESPONSE POINTER: \n"); |
| 174 | + } |
| 175 | + |
| 176 | + HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n"); |
| 177 | + while (client->connected()) { |
| 178 | + HTTP_DEBUG_PRINT("."); |
| 179 | + |
| 180 | + if (client->available()) { |
| 181 | + HTTP_DEBUG_PRINT(","); |
| 182 | + |
| 183 | + char c = client->read(); |
| 184 | + HTTP_DEBUG_PRINT(c); |
| 185 | + |
| 186 | + if(c == ' ' && !inStatus){ |
| 187 | + inStatus = true; |
| 188 | + } |
| 189 | + |
| 190 | + if(inStatus && i < 3 && c != ' '){ |
| 191 | + statusCode[i] = c; |
| 192 | + i++; |
| 193 | + } |
| 194 | + if(i == 3){ |
| 195 | + statusCode[i] = '\0'; |
| 196 | + code = atoi(statusCode); |
| 197 | + } |
| 198 | + |
| 199 | + if(httpBody){ |
| 200 | + //only write response if its not null |
| 201 | + if(response != NULL) response->concat(c); |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + if (c == '\n' && currentLineIsBlank) { |
| 206 | + httpBody = true; |
| 207 | + } |
| 208 | + |
| 209 | + if (c == '\n') { |
| 210 | + // you're starting a new line |
| 211 | + currentLineIsBlank = true; |
| 212 | + } |
| 213 | + else if (c != '\r') { |
| 214 | + // you've gotten a character on the current line |
| 215 | + currentLineIsBlank = false; |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + HTTP_DEBUG_PRINT("HTTP: return readResponse3\n"); |
| 222 | + return code; |
| 223 | +} |
0 commit comments