|
| 1 | +#include <iostream> |
| 2 | +#include <nabto/webrtc/util/logging.hpp> |
| 3 | +#include "lws_http_client.hpp" |
| 4 | +#include "util.hpp" |
| 5 | + |
| 6 | +namespace nabto::example { |
| 7 | + |
| 8 | +std::unordered_map<uint64_t, LwsHttpClient::Request> LwsHttpClient::requests_; |
| 9 | + |
| 10 | +LwsHttpClient::LwsHttpClient() { |
| 11 | +} |
| 12 | + |
| 13 | +LwsHttpClient::~LwsHttpClient() { |
| 14 | + cleanup(); |
| 15 | +} |
| 16 | + |
| 17 | +void LwsHttpClient::cleanup() { |
| 18 | +} |
| 19 | + |
| 20 | +bool LwsHttpClient::sendRequest(const nabto::webrtc::SignalingHttpRequest& request, |
| 21 | + nabto::webrtc::HttpResponseCallback cb) { |
| 22 | + std::lock_guard<std::mutex> lock(sendMutex_); |
| 23 | + |
| 24 | + auto parsed = parseUrl(request.url); |
| 25 | + auto protocol = std::get<0>(parsed); |
| 26 | + auto host = std::get<1>(parsed); |
| 27 | + auto path = std::get<2>(parsed); |
| 28 | + |
| 29 | + struct lws_client_connect_info ccinfo = {}; |
| 30 | + ccinfo.context = contextManager_->getContext(); |
| 31 | + |
| 32 | + ccinfo.ssl_connection = LCCSCF_USE_SSL; |
| 33 | + ccinfo.port = 443; |
| 34 | + ccinfo.address = host.c_str(); |
| 35 | + ccinfo.path = path.c_str(); |
| 36 | + ccinfo.host = ccinfo.address; |
| 37 | + ccinfo.origin = ccinfo.address; |
| 38 | + ccinfo.method = request.method.c_str(); |
| 39 | + ccinfo.alpn = "http/1.1"; |
| 40 | + |
| 41 | + NPLOGI << request.method << " " << request.body; |
| 42 | + |
| 43 | + uint64_t handle = requestIndex_++; |
| 44 | + requests_[handle] = { this, cb, request, "", 0, 0 }; |
| 45 | + requests_[handle].req.headers.push_back({ "Content-Length", std::to_string(request.body.length()) }); |
| 46 | + |
| 47 | + ccinfo.protocol = "lws-http-protocol"; |
| 48 | + ccinfo.userdata = reinterpret_cast<void*>(handle); |
| 49 | + ccinfo.userdata = (void*)handle; |
| 50 | + ccinfo.pwsi = &requests_[handle].wsi; |
| 51 | + |
| 52 | + if (!lws_client_connect_via_info(&ccinfo)) { |
| 53 | + NPLOGE << "Client creation failed!"; |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +int LwsHttpClient::lwsCallback(struct lws* wsi, enum lws_callback_reasons reason, |
| 61 | + void* user, void* in, size_t len) { |
| 62 | + uint64_t handle = (uint64_t)user; |
| 63 | + Request& r = requests_[handle]; |
| 64 | + |
| 65 | + char buf[LWS_PRE + 1024]; |
| 66 | + char* start = &buf[LWS_PRE]; |
| 67 | + char* end = &buf[sizeof(buf) - 1]; |
| 68 | + char* p = start; |
| 69 | + int n; |
| 70 | + |
| 71 | + switch (reason) { |
| 72 | + case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: { |
| 73 | + NPLOGI << "CLIENT_CONNECTION_ERROR"; |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + case LWS_CALLBACK_CLOSED_CLIENT_HTTP: { |
| 78 | + NPLOGI << "CLOSED_CLIENT_HTTP"; |
| 79 | + requests_.erase(handle); |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + // Receiving callbacks |
| 84 | + |
| 85 | + case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: { |
| 86 | + int status = lws_http_client_http_response(wsi); |
| 87 | + NPLOGI << "ESTABLISHED_CLIENT_HTTP : " << status; |
| 88 | + r.statusCode = status; |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: { |
| 93 | + NPLOGI << "RECEIVE_CLIENT_HTTP_READ : read " << len; |
| 94 | + std::string chunk(static_cast<const char*>(in), len); |
| 95 | + r.body += chunk; |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + case LWS_CALLBACK_RECEIVE_CLIENT_HTTP: { |
| 100 | + n = sizeof(buf) - LWS_PRE; |
| 101 | + if (lws_http_client_read(wsi, &p, &n) < 0) { |
| 102 | + return -1; |
| 103 | + } |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + case LWS_CALLBACK_COMPLETED_CLIENT_HTTP: { |
| 108 | + NPLOGI << "COMPLETED_CLIENT_HTTP : status " << r.statusCode << " : " << r.body; |
| 109 | + auto response = std::make_unique<nabto::webrtc::SignalingHttpResponse>(); |
| 110 | + response->statusCode = r.statusCode; |
| 111 | + response->body = r.body; |
| 112 | + r.cb(std::move(response)); |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + // Callbacks for generating POST data |
| 117 | + |
| 118 | + case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: { |
| 119 | + if (!lws_http_is_redirected_to_get(wsi)) { |
| 120 | + NPLOGI << "APPEND_HANDSHAKE_HEADER : doing POST flow"; |
| 121 | + |
| 122 | + unsigned char **headerPtr = (unsigned char**)in; |
| 123 | + unsigned char *headerEnd = (*headerPtr) + len; |
| 124 | + |
| 125 | + for (auto& element : r.req.headers) { |
| 126 | + lws_add_http_header_by_name( |
| 127 | + wsi, |
| 128 | + (unsigned char*)element.first.c_str(), |
| 129 | + (unsigned char*)element.second.c_str(), |
| 130 | + element.second.length(), |
| 131 | + headerPtr, |
| 132 | + headerEnd |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + lws_client_http_body_pending(wsi, 1); |
| 137 | + lws_callback_on_writable(wsi); |
| 138 | + } else { |
| 139 | + NPLOGI << "APPEND_HANDSHAKE_HEADER : doing GET flow"; |
| 140 | + } |
| 141 | + |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE: { |
| 146 | + NPLOGI << "CLIENT_HTTP_WRITEABLE"; |
| 147 | + |
| 148 | + if (lws_http_is_redirected_to_get(wsi)) { |
| 149 | + break; |
| 150 | + } |
| 151 | + |
| 152 | + enum lws_write_protocol prot = LWS_WRITE_HTTP; |
| 153 | + size_t remaining = r.req.body.length() - r.writtenBytes; |
| 154 | + size_t chunkSize = std::min(remaining, (size_t)1024); |
| 155 | + |
| 156 | + memcpy(start, r.req.body.c_str() + r.writtenBytes, chunkSize); |
| 157 | + r.writtenBytes += chunkSize; |
| 158 | + |
| 159 | + if (r.writtenBytes >= r.req.body.length()) { |
| 160 | + prot = LWS_WRITE_HTTP_FINAL; |
| 161 | + } |
| 162 | + |
| 163 | + int written = lws_write(wsi, (uint8_t*)start, chunkSize, prot); |
| 164 | + |
| 165 | + if (written != chunkSize) { |
| 166 | + return 1; |
| 167 | + } |
| 168 | + |
| 169 | + if (prot != LWS_WRITE_HTTP_FINAL) { |
| 170 | + lws_callback_on_writable(wsi); |
| 171 | + } else { |
| 172 | + lws_client_http_body_pending(wsi, 0); |
| 173 | + } |
| 174 | + |
| 175 | + return 0; |
| 176 | + } |
| 177 | + |
| 178 | + default: { |
| 179 | + break; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return lws_callback_http_dummy(wsi, reason, user, in, len); |
| 184 | +} |
| 185 | + |
| 186 | +} // nabto::webrtc |
0 commit comments