GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
http.cpp@1:27b35752c5d0, 2014-11-18 (annotated)
- Committer:
- Trumple
- Date:
- Tue Nov 18 18:28:52 2014 +0000
- Revision:
- 1:27b35752c5d0
- Child:
- 2:1cbb20dd1733
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Trumple | 1:27b35752c5d0 | 1 | #include "mbed.h" |
Trumple | 1:27b35752c5d0 | 2 | #include "http.h" |
Trumple | 1:27b35752c5d0 | 3 | #include <string> |
Trumple | 1:27b35752c5d0 | 4 | #include <vector> |
Trumple | 1:27b35752c5d0 | 5 | #include <sstream> |
Trumple | 1:27b35752c5d0 | 6 | |
Trumple | 1:27b35752c5d0 | 7 | http::http() |
Trumple | 1:27b35752c5d0 | 8 | { |
Trumple | 1:27b35752c5d0 | 9 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 10 | printf("[HTTP] Ethernet connecting...\r\n"); |
Trumple | 1:27b35752c5d0 | 11 | #endif |
Trumple | 1:27b35752c5d0 | 12 | |
Trumple | 1:27b35752c5d0 | 13 | eth.init(); |
Trumple | 1:27b35752c5d0 | 14 | eth.connect(); |
Trumple | 1:27b35752c5d0 | 15 | |
Trumple | 1:27b35752c5d0 | 16 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 17 | printf("[HTTP] Ethernet connected, IP: %s\r\n", eth.getIPAddress()); |
Trumple | 1:27b35752c5d0 | 18 | #endif |
Trumple | 1:27b35752c5d0 | 19 | } |
Trumple | 1:27b35752c5d0 | 20 | |
Trumple | 1:27b35752c5d0 | 21 | string http::get(string address, int port, string url, int replyTimeout) |
Trumple | 1:27b35752c5d0 | 22 | { |
Trumple | 1:27b35752c5d0 | 23 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 24 | printf("[HTTP] Sending GET request to %s%s\r\n", address.c_str(), url.c_str()); |
Trumple | 1:27b35752c5d0 | 25 | #endif |
Trumple | 1:27b35752c5d0 | 26 | |
Trumple | 1:27b35752c5d0 | 27 | TCPSocketConnection sock; |
Trumple | 1:27b35752c5d0 | 28 | sock.connect(address.c_str(), port); |
Trumple | 1:27b35752c5d0 | 29 | |
Trumple | 1:27b35752c5d0 | 30 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 31 | printf("[HTTP] Connected to endpoint...\r\n"); |
Trumple | 1:27b35752c5d0 | 32 | #endif |
Trumple | 1:27b35752c5d0 | 33 | |
Trumple | 1:27b35752c5d0 | 34 | string httpget = "GET " + url + " HTTP/1.1"; |
Trumple | 1:27b35752c5d0 | 35 | httpget += "\nHost: " + address + "\n\n"; |
Trumple | 1:27b35752c5d0 | 36 | |
Trumple | 1:27b35752c5d0 | 37 | //get a writable char* (I.E. not const char* returned by c_str), put it into a vector |
Trumple | 1:27b35752c5d0 | 38 | vector<char> writable(httpget.begin(), httpget.end()); |
Trumple | 1:27b35752c5d0 | 39 | writable.push_back('\0'); |
Trumple | 1:27b35752c5d0 | 40 | |
Trumple | 1:27b35752c5d0 | 41 | sock.send_all(&writable[0], writable.size()-1); |
Trumple | 1:27b35752c5d0 | 42 | |
Trumple | 1:27b35752c5d0 | 43 | string message = this->receiveFromSock(sock, replyTimeout); |
Trumple | 1:27b35752c5d0 | 44 | |
Trumple | 1:27b35752c5d0 | 45 | sock.close(); |
Trumple | 1:27b35752c5d0 | 46 | |
Trumple | 1:27b35752c5d0 | 47 | return this->parse(message); |
Trumple | 1:27b35752c5d0 | 48 | } |
Trumple | 1:27b35752c5d0 | 49 | |
Trumple | 1:27b35752c5d0 | 50 | string http::post(string address, int port, string url, string jsonPayload, int replyTimeout) |
Trumple | 1:27b35752c5d0 | 51 | { |
Trumple | 1:27b35752c5d0 | 52 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 53 | printf("[HTTP] Sending POST request to %s%s\r\n", address.c_str(), url.c_str()); |
Trumple | 1:27b35752c5d0 | 54 | #endif |
Trumple | 1:27b35752c5d0 | 55 | |
Trumple | 1:27b35752c5d0 | 56 | TCPSocketConnection sock; |
Trumple | 1:27b35752c5d0 | 57 | sock.connect(address.c_str(), port); |
Trumple | 1:27b35752c5d0 | 58 | |
Trumple | 1:27b35752c5d0 | 59 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 60 | printf("[HTTP] Connected to endpoint...\r\n"); |
Trumple | 1:27b35752c5d0 | 61 | #endif |
Trumple | 1:27b35752c5d0 | 62 | |
Trumple | 1:27b35752c5d0 | 63 | stringstream contentLength; |
Trumple | 1:27b35752c5d0 | 64 | contentLength << jsonPayload.size(); |
Trumple | 1:27b35752c5d0 | 65 | string contentLengthStr = contentLength.str(); |
Trumple | 1:27b35752c5d0 | 66 | |
Trumple | 1:27b35752c5d0 | 67 | string httppost = "POST " + url + " HTTP/1.1"; |
Trumple | 1:27b35752c5d0 | 68 | httppost += "\nHost: " + address; |
Trumple | 1:27b35752c5d0 | 69 | httppost += "\nContent-Type: application/json"; |
Trumple | 1:27b35752c5d0 | 70 | httppost += "\nContent-Length: " + contentLengthStr; |
Trumple | 1:27b35752c5d0 | 71 | httppost += "\n\n" + jsonPayload; |
Trumple | 1:27b35752c5d0 | 72 | |
Trumple | 1:27b35752c5d0 | 73 | //to get a writable char* (I.E. not const char* returned by string.c_str), put it into a vector |
Trumple | 1:27b35752c5d0 | 74 | vector<char> writable(httppost.begin(), httppost.end()); |
Trumple | 1:27b35752c5d0 | 75 | writable.push_back('\0'); |
Trumple | 1:27b35752c5d0 | 76 | |
Trumple | 1:27b35752c5d0 | 77 | sock.send_all(&writable[0], writable.size()-1); |
Trumple | 1:27b35752c5d0 | 78 | |
Trumple | 1:27b35752c5d0 | 79 | string message = this->receiveFromSock(sock, replyTimeout); |
Trumple | 1:27b35752c5d0 | 80 | |
Trumple | 1:27b35752c5d0 | 81 | sock.close(); |
Trumple | 1:27b35752c5d0 | 82 | |
Trumple | 1:27b35752c5d0 | 83 | return this->parse(message); |
Trumple | 1:27b35752c5d0 | 84 | } |
Trumple | 1:27b35752c5d0 | 85 | |
Trumple | 1:27b35752c5d0 | 86 | string http::receiveFromSock(TCPSocketConnection sock, int replyTimeout) |
Trumple | 1:27b35752c5d0 | 87 | { |
Trumple | 1:27b35752c5d0 | 88 | char buffer[1024]; |
Trumple | 1:27b35752c5d0 | 89 | int receiveByteCount; |
Trumple | 1:27b35752c5d0 | 90 | string message; |
Trumple | 1:27b35752c5d0 | 91 | |
Trumple | 1:27b35752c5d0 | 92 | while (true) |
Trumple | 1:27b35752c5d0 | 93 | { |
Trumple | 1:27b35752c5d0 | 94 | receiveByteCount = sock.receive(buffer, sizeof(buffer)-1);//spare a byte for null termination byte |
Trumple | 1:27b35752c5d0 | 95 | if (receiveByteCount <= 0) |
Trumple | 1:27b35752c5d0 | 96 | break; |
Trumple | 1:27b35752c5d0 | 97 | buffer[receiveByteCount] = '\0'; |
Trumple | 1:27b35752c5d0 | 98 | message += buffer; |
Trumple | 1:27b35752c5d0 | 99 | } |
Trumple | 1:27b35752c5d0 | 100 | |
Trumple | 1:27b35752c5d0 | 101 | return message; |
Trumple | 1:27b35752c5d0 | 102 | } |
Trumple | 1:27b35752c5d0 | 103 | |
Trumple | 1:27b35752c5d0 | 104 | string http::parse(string httpReply) |
Trumple | 1:27b35752c5d0 | 105 | { |
Trumple | 1:27b35752c5d0 | 106 | return httpReply; |
Trumple | 1:27b35752c5d0 | 107 | } |