irmecir

HTTPSRedirect.cpp

Oct 8th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /* HTTPS with follow-redirect
  2. * Created by Sujay S. Phadke, 2016
  3. * All rights reserved.
  4. *
  5. */
  6. // to-do: Use 'F' macro for storing strings
  7. // to-do: remove spurious characters in the message body
  8.  
  9. #include "HTTPSRedirect.h"
  10. #include "Arduino.h"
  11.  
  12. // from LarryD, Arduino forum
  13. //#define DEBUG //If you comment this line, the DPRINT & DPRINTLN lines are defined as blank.
  14. #ifdef DEBUG //Macros are usually in all capital letters.
  15. #define DPRINT(...) Serial.print(__VA_ARGS__) //DPRINT is a macro, debug print
  16. #define DPRINTLN(...) Serial.println(__VA_ARGS__) //DPRINTLN is a macro, debug print with new line
  17. #else
  18. #define DPRINT(...) //now defines a blank line
  19. #define DPRINTLN(...) //now defines a blank line
  20. #endif
  21.  
  22. //pinMode(2, OUTPUT);
  23.  
  24. HTTPSRedirect::HTTPSRedirect(const int p, const char* fp, bool c) : httpsPort(p), redirFingerprint(fp), fpCheck(c){
  25. }
  26.  
  27. HTTPSRedirect::HTTPSRedirect(const int p) : httpsPort(p){
  28. fpCheck = false;
  29. }
  30.  
  31. HTTPSRedirect::~HTTPSRedirect(){
  32. }
  33.  
  34. bool HTTPSRedirect::printRedir(String& url, const char* host, const char* redirHost){
  35. return printRedir(url.c_str(), host, redirHost);
  36. }
  37.  
  38. bool HTTPSRedirect::printRedir(const char* url, const char* host, const char* redirHost){
  39.  
  40. int redirFlag = false;
  41.  
  42. // Check if connection to host is alive
  43. if (!connected()){
  44. Serial.println("Error! Not connected to host.");
  45. return false;
  46. }
  47. // HTTP headers must be terminated with a "\r\n\r\n"
  48. // http://stackoverflow.com/questions/6686261/what-at-the-bare-minimum-is-required-for-an-http-request
  49. // http://serverfault.com/questions/163511/what-is-the-mandatory-information-a-http-request-header-must-contain
  50. String Request = createRequest(url, host);
  51.  
  52. DPRINTLN(Request);
  53. // make request
  54. print(Request);
  55.  
  56. String line;
  57. String redirUrl;
  58.  
  59. DPRINTLN("Detecting re-direction.");
  60. DPRINTLN(redirHost);
  61.  
  62. while (connected()) {
  63. line = readStringUntil('\n');
  64. DPRINTLN(line);
  65. if (line == "\r") {
  66. DPRINTLN("END OF HEADER");
  67. //DPRINTLN(line);
  68. break;
  69. }
  70.  
  71. if (find("Location: ") ){
  72. find((char *)redirHost);
  73. DPRINTLN("Found re-direction URL!");
  74. redirUrl = readStringUntil('\n');
  75. redirFlag = true;
  76. break;
  77. }
  78. /*
  79. if (finder.findUntil("chunked", "\n\r") ){
  80. break;
  81. }*/
  82. }
  83.  
  84. DPRINTLN("Body:\n");
  85. if (verboseInfo)
  86. fetchData(true, false);
  87. else
  88. flush();
  89.  
  90. if (!redirFlag){
  91. DPRINTLN("No re-direction URL found in header.");
  92. return false;
  93. }
  94. //flush();
  95. //stop();
  96.  
  97. DPRINTLN("Redirected URL:");
  98. DPRINTLN(redirUrl);
  99.  
  100. Request = createRequest(redirUrl.c_str(), redirHost);
  101.  
  102. DPRINTLN("Connecting to:");
  103. DPRINTLN(redirHost);
  104.  
  105. if (!connect(redirHost, httpsPort)) {
  106. Serial.println("Connection to re-directed host failed!");
  107. return 0;
  108. }
  109.  
  110. if (fpCheck){
  111. if (verify(redirFingerprint, redirHost)) {
  112. Serial.println("Re-directed host certificate match.");
  113. } else {
  114. Serial.println("Re-directed host certificate mis-match");
  115. }
  116. }
  117.  
  118. DPRINTLN("Requesting re-directed URL.");
  119. DPRINTLN(Request);
  120.  
  121. // Make request
  122. print(Request);
  123.  
  124. DPRINTLN("Final Response:");
  125.  
  126. fetchData(false, true);
  127.  
  128. fetchData(true, false);
  129.  
  130. flush();
  131. //stop();
  132.  
  133. return true;
  134. }
  135.  
  136. String HTTPSRedirect::createRequest(const char* url, const char* host){
  137. return String("GET ") + url + " HTTP/1.1\r\n" +
  138. "Host: " + host + "\r\n" +
  139. "User-Agent: ESP8266\r\n" +
  140. (keepAlive ? "" : "Connection: close") +
  141. "\r\n\r\n";
  142.  
  143. }
  144.  
  145. void HTTPSRedirect::fetchData(bool disp, bool header){
  146.  
  147. String line;
  148.  
  149. pinMode(2, OUTPUT);
  150.  
  151. while (connected()) {
  152. line = readStringUntil('\n');
  153.  
  154. if (disp)
  155. Serial.println(line);
  156. if(line == "On\r"){
  157. //Serial.println("Led On");
  158. digitalWrite(2, HIGH);
  159. }
  160. if(line == "Off\r"){
  161. //Serial.println("Led Off");
  162. digitalWrite(2, LOW);
  163. }
  164.  
  165. if (line == "\r") {
  166. if (disp){
  167. if (header) DPRINTLN("END OF HEADER");
  168. else DPRINTLN("END OF RESPONSE");
  169. //DPRINTLN(line);
  170. }
  171. break;
  172. }
  173. }
  174. myLine = line;
  175.  
  176. //Serial.println(client.myLine);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment