HTTPSClient.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "HTTPSClient.h"
  2. #include "HTTPHeader.h"
  3. #include <string>
  4. #include <cstring>
  5. #include "mbed.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. using std::memset;
  9. using std::memcpy;
  10. using std::string;
  11. const static int HTTPS_PORT = 443;
  12. char buf[256];
  13. HTTPSClient::HTTPSClient() :
  14. _is_connected(false),
  15. _ssl_ctx(),
  16. _ssl(),
  17. _host() {
  18. }
  19. HTTPSClient::~HTTPSClient() {
  20. close();
  21. }
  22. int HTTPSClient::connect(const char* host) {
  23. if (init_socket(SOCK_STREAM) < 0)
  24. return -1;
  25. if (set_address(host, HTTPS_PORT) != 0)
  26. return -1;
  27. if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
  28. close();
  29. return -1;
  30. }
  31. if(ssl_ctx_new(&_ssl_ctx, SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS) != &_ssl_ctx)
  32. return -1;
  33. _ssl.ssl_ctx = &_ssl_ctx;
  34. if(ssl_client_new(&_ssl, _sock_fd, NULL, 0) == NULL)
  35. {
  36. close();
  37. return -1;
  38. }
  39. if(_ssl.hs_status != SSL_OK)
  40. {
  41. close();
  42. return -1;
  43. }
  44. _is_connected = true;
  45. _host = host;
  46. return 0;
  47. }
  48. bool HTTPSClient::is_connected(void) {
  49. return _is_connected;
  50. }
  51. int HTTPSClient::send(char* data, int length) {
  52. if ((_sock_fd < 0) || !_is_connected)
  53. return -1;
  54. return ssl_write(&_ssl, (uint8_t*)data, length);
  55. }
  56. HTTPHeader HTTPSClient::get(char *request)
  57. {
  58. if((_sock_fd < 0) || !_is_connected)
  59. return HTTPHeader();
  60. sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", request, _host.c_str());
  61. printf("buf=%s\n", buf);
  62. if(send(buf, strlen(buf)) != strlen(buf))
  63. return HTTPHeader();
  64. printf("Finished sending request\n");
  65. return read_header();
  66. }
  67. HTTPHeader HTTPSClient::read_header()
  68. {
  69. _ssl.bm_read_index = 0;
  70. _ssl.bm_index = 0;
  71. HTTPHeader hdr;
  72. if(read_line())
  73. return hdr;
  74. int status;
  75. if(sscanf(buf, "HTTP/%*d.%*d %d %*s", &status) == -1)
  76. return hdr;
  77. if(status == 200)
  78. hdr._status = HTTP_OK;
  79. if(read_line())
  80. return hdr;
  81. do
  82. {
  83. string tmp(buf);
  84. std::size_t sep = tmp.find(':');
  85. string name = tmp.substr(0, sep);
  86. string value = tmp.substr(sep+2, tmp.size());
  87. hdr._fields[name] = value;
  88. if(read_line())
  89. return hdr;
  90. }while(strlen(buf));
  91. return hdr;
  92. }
  93. uint8_t HTTPSClient::read_line()
  94. {
  95. int index = 0;
  96. do
  97. {
  98. if(ssl_read(&_ssl, (uint8_t*)(&buf[index]), 1) != 1)
  99. {
  100. return 1;
  101. }
  102. index++;
  103. }while(buf[index-1] != '\r' && index < 256);
  104. ssl_read(&_ssl, (uint8_t*)(&buf[index-1]), 1); // skip '\n'
  105. buf[index-1] = '\0';
  106. return 0;
  107. }
  108. // -1:error
  109. // otherwise return nb of characters read. Cannot be > than len
  110. int HTTPSClient::read(char *data, int len)
  111. {
  112. return ssl_read(&_ssl, (uint8_t*)data, len);
  113. }
  114. /*
  115. 0 : must close connection
  116. -1 : error
  117. else : get data
  118. int HTTPSClient::receive(char* data, int length) {
  119. if ((_sock_fd < 0) || !_is_connected)
  120. return -1;
  121. if(read_record(&_ssl) < 0)
  122. return -1;
  123. return process_data(&_ssl, (uint8_t*)data, length);
  124. }
  125. */
  126. void HTTPSClient::close()
  127. {
  128. if(!_is_connected)
  129. return;
  130. ssl_ctx_free(_ssl.ssl_ctx);
  131. Socket::close();
  132. _is_connected = false;
  133. _host.clear();
  134. }