| 128 | } |
| 129 | |
| 130 | void HttpServiceWorker::performHttpRequest(const WorkerRequest& request, WorkerResponse& response, Mutex& mutex, HttpJobId& jobToAbort) |
| 131 | { |
| 132 | const char* PREFIX_HTTPS = "https://"; |
| 133 | WiFiClient* wifiClient = nullptr; |
| 134 | |
| 135 | /* HTTP over TLS required? */ |
| 136 | if (true == request.url.startsWith(PREFIX_HTTPS)) |
| 137 | { |
| 138 | WiFiClientSecure* secureClient = new WiFiClientSecure(); |
| 139 | |
| 140 | if (nullptr != secureClient) |
| 141 | { |
| 142 | wifiClient = secureClient; |
| 143 | secureClient->setInsecure(); |
| 144 | } |
| 145 | } |
| 146 | /* HTTP over plain TCP. */ |
| 147 | else |
| 148 | { |
| 149 | wifiClient = new WiFiClient(); |
| 150 | } |
| 151 | |
| 152 | if (nullptr == wifiClient) |
| 153 | { |
| 154 | LOG_WARNING("HTTP request to URL %s failed, no heap memory available.", request.url.c_str()); |
| 155 | response.statusCode = HTTP_CODE_INTERNAL_SERVER_ERROR; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | HTTPClient httpClient; |
| 160 | |
| 161 | httpClient.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); |
| 162 | httpClient.setRedirectLimit(5U); |
| 163 | |
| 164 | if (false == httpClient.begin(*wifiClient, request.url)) |
| 165 | { |
| 166 | LOG_WARNING("HTTP request failed to %s", request.url.c_str()); |
| 167 | response.statusCode = HTTP_CODE_SERVICE_UNAVAILABLE; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | int httpClientRet = 0; |
| 172 | |
| 173 | LOG_DEBUG("HTTP client connected."); |
| 174 | |
| 175 | switch (request.method) |
| 176 | { |
| 177 | case HTTP_METHOD_GET: |
| 178 | httpClientRet = httpClient.GET(); |
| 179 | break; |
| 180 | |
| 181 | case HTTP_METHOD_POST: |
| 182 | httpClientRet = httpClient.POST(const_cast<uint8_t*>(request.payload), request.size); |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | LOG_WARNING("HTTP request to %s failed.", request.url.c_str()); |
| 187 | LOG_WARNING("Unsupported HTTP method %d.", request.method); |