Advertisement
Guest User

connect

a guest
Oct 16th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file    rest_client.cpp
  4.  *
  5.  * details: https://github.com/llad/spark-restclient
  6.  *
  7.  * credit: https://github.com/csquared/arduino-restclient
  8.  *
  9.  ******************************************************************************
  10.  
  11. */
  12.  
  13. #include "rest_client.h"
  14.  
  15. //#define HTTP_DEBUG
  16.  
  17. #ifdef HTTP_DEBUG
  18. #define HTTP_DEBUG_PRINT(string) (Serial.print(string))
  19. #endif
  20.  
  21. #ifndef HTTP_DEBUG
  22. #define HTTP_DEBUG_PRINT(string)
  23. #endif
  24.  
  25. RestClient::RestClient(uint8_t* _host){
  26.    
  27.   host[0] = _host[0];
  28.   host[1] = _host[1];
  29.   host[2] = _host[2];
  30.   host[3] = _host[3];
  31.   port = 80;
  32.   num_headers = 0;
  33.   contentTypeSet = false;
  34. }
  35.  
  36. RestClient::RestClient(uint8_t* _host, int _port){
  37.   host[0] = _host[0];
  38.   host[1] = _host[1];
  39.   host[2] = _host[2];
  40.   host[3] = _host[3];
  41.   port = _port;
  42.   num_headers = 0;
  43.   contentTypeSet = false;
  44. }
  45.  
  46. // GET path
  47. int RestClient::get(const char* path){
  48.   return request("GET", path, NULL, NULL);
  49. }
  50.  
  51. //GET path with response
  52. int RestClient::get(const char* path, String* response){
  53.   return request("GET", path, NULL, response);
  54. }
  55.  
  56. // POST path and body
  57. int RestClient::post(const char* path, const char* body){
  58.   return request("POST", path, body, NULL);
  59. }
  60.  
  61. // POST path and body with response
  62. int RestClient::post(const char* path, const char* body, String* response){
  63.   return request("POST", path, body, response);
  64. }
  65.  
  66. // PUT path and body
  67. int RestClient::put(const char* path, const char* body){
  68.   return request("PUT", path, body, NULL);
  69. }
  70.  
  71. // PUT path and body with response
  72. int RestClient::put(const char* path, const char* body, String* response){
  73.   return request("PUT", path, body, response);
  74. }
  75.  
  76. // DELETE path
  77. int RestClient::del(const char* path){
  78.   return request("DELETE", path, NULL, NULL);
  79. }
  80.  
  81. // DELETE path and response
  82. int RestClient::del(const char* path, String* response){
  83.   return request("DELETE", path, NULL, response);
  84. }
  85.  
  86. // DELETE path and body
  87. int RestClient::del(const char* path, const char* body ){
  88.   return request("DELETE", path, body, NULL);
  89. }
  90.  
  91. // DELETE path and body with response
  92. int RestClient::del(const char* path, const char* body, String* response){
  93.   return request("DELETE", path, body, response);
  94. }
  95.  
  96. void RestClient::write(const char* string){
  97.   HTTP_DEBUG_PRINT(string);
  98.   client.print(string);
  99. }
  100.  
  101. void RestClient::setHeader(const char* header){
  102.   headers[num_headers] = header;
  103.   num_headers++;
  104. }
  105.  
  106. // The mother- generic request method.
  107. //
  108. int RestClient::request(const char* method, const char* path,
  109.                   const char* body, String* response){
  110.                      
  111.     if(last_contact!=0 && last_contact+2500>millis()){
  112.         delay(last_contact+2500-millis());
  113.     }
  114.     last_contact=millis();
  115.  
  116.   const char HOSTNAME[5]={host[0],host[1],host[2],host[3],0x00};
  117.  
  118.   HTTP_DEBUG_PRINT("HTTP: connect\n");
  119.  
  120.   Serial.println("und los");
  121.   delay(200);
  122.   Serial.print("connect to ");
  123.   Serial.print(host[0],DEC);
  124.   Serial.print(".");
  125.   Serial.print(host[1],DEC);
  126.   Serial.print(".");
  127.   Serial.print(host[2],DEC);
  128.   Serial.print(".");
  129.   Serial.print(host[3],DEC);
  130.   Serial.println("!");
  131.   //delay(2000);
  132.  
  133.   if(client.connect(host, port)){
  134.      
  135.     delay(200);
  136.     Serial.println("connected");
  137.     delay(200);
  138.  
  139.     HTTP_DEBUG_PRINT("HTTP: connected\n");
  140.     HTTP_DEBUG_PRINT("REQUEST: \n");
  141.     // Make a HTTP request line:
  142.     write(method);
  143.     write(" ");
  144.     write(path);
  145.     write(" HTTP/1.1\r\n");
  146.     for(int i=0; i<num_headers; i++){
  147.       write(headers[i]);
  148.       write("\r\n");
  149.     }
  150.     write("Host: ");
  151.     write(HOSTNAME);
  152.     write("\r\n");
  153.     write("Connection: close\r\n");
  154.  
  155.     if(body != NULL){
  156.       char contentLength[30];
  157.       sprintf(contentLength, "Content-Length: %d\r\n", strlen(body));
  158.       write(contentLength);
  159.  
  160.       if(!contentTypeSet){
  161.         write("Content-Type: application/x-www-form-urlencoded\r\n");
  162.       }
  163.     }
  164.  
  165.     write("\r\n");
  166.  
  167.     if(body != NULL){
  168.       write(body);
  169.       write("\r\n");
  170.       write("\r\n");
  171.     }
  172.  
  173.     //make sure you write all those bytes.
  174.     delay(200);
  175.  
  176.     HTTP_DEBUG_PRINT("HTTP: call readResponse\n");
  177.     int statusCode = readResponse(response);
  178.     HTTP_DEBUG_PRINT("HTTP: return readResponse\n");
  179.  
  180.     //cleanup
  181.     HTTP_DEBUG_PRINT("HTTP: stop client\n");
  182.     num_headers = 0;
  183.     client.stop();
  184.     delay(50);
  185.     HTTP_DEBUG_PRINT("HTTP: client stopped\n");
  186.  
  187.     return statusCode;
  188.   }else{
  189.     HTTP_DEBUG_PRINT("HTTP Connection failed\n");
  190.     return 0;
  191.   }
  192. }
  193.  
  194. int RestClient::readResponse(String* response) {
  195.  
  196.   // an http request ends with a blank line
  197.   boolean currentLineIsBlank = true;
  198.   boolean httpBody = false;
  199.   boolean inStatus = false;
  200.  
  201.   char statusCode[4];
  202.   int i = 0;
  203.   int code = 0;
  204.  
  205.   if(response == NULL){
  206.     HTTP_DEBUG_PRINT("HTTP: NULL RESPONSE POINTER: \n");
  207.   }else{
  208.     HTTP_DEBUG_PRINT("HTTP: NON-NULL RESPONSE POINTER: \n");
  209.   }
  210.  
  211.   HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n");
  212.   uint32_t now=millis();
  213.   while (client.connected() && millis()<now+2000) {
  214.     HTTP_DEBUG_PRINT(".");
  215.     if (client.available()) {
  216.  
  217.       char c = client.read();
  218.       HTTP_DEBUG_PRINT(c);
  219.  
  220.       if(c == ' ' && !inStatus){
  221.         inStatus = true;
  222.       }
  223.  
  224.       if(inStatus && i < 3 && c != ' '){
  225.         statusCode[i] = c;
  226.         i++;
  227.       }
  228.       if(i == 3){
  229.         statusCode[i] = '\0';
  230.         code = atoi(statusCode);
  231.       }
  232.  
  233.       //only write response if its not null
  234.       if(httpBody){
  235.         if(response != NULL) response->concat(c);
  236.       }
  237.       if (c == '\n' && httpBody){
  238.         HTTP_DEBUG_PRINT("HTTP: return readResponse2\n");
  239.         //return code;
  240.       }
  241.       if (c == '\n' && currentLineIsBlank) {
  242.         httpBody = true;
  243.         HTTP_DEBUG_PRINT("Here comes the body\n");
  244.       }
  245.       if (c == '\n') {
  246.         // you're starting a new lineu
  247.         currentLineIsBlank = true;
  248.       }
  249.       else if (c != '\r') {
  250.         // you've gotten a character on the current line
  251.         currentLineIsBlank = false;
  252.       }
  253.     }
  254.     //delay(100);
  255.   }
  256.   client.flush();
  257.  
  258.   HTTP_DEBUG_PRINT("HTTP: return readResponse3\n");
  259.   return code;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement