Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- req->headers->value = std::string_view(req->headers->value.data(), std::max<int>(0, req->headers->value.length() - 9));
- // the only caller of getHeaders
- template <int CONSUME_MINIMALLY>
- std::pair<int, void *> fenceAndConsumePostPadded(char *data, int length, void *user, HttpRequest *req, fu2::unique_function<void *(void *, HttpRequest *)> &requestHandler, fu2::unique_function<void *(void *, std::string_view, bool)> &dataHandler) {
- int consumedTotal = 0;
- data[length] = 'r';
- for (int consumed; length && (consumed = getHeaders(data, data + length, req->headers)); ) {
- data += consumed;
- length -= consumed;
- consumedTotal += consumed;
- req->headers->value = std::string_view(req->headers->value.data(), std::max<int>(0, req->headers->value.length() - 9));
- /* Parse query */
- const char *querySeparatorPtr = (const char *) memchr(req->headers->value.data(), '?', req->headers->value.length());
- req->querySeparator = (querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data();
- /* If returned socket is not what we put in we need
- * to break here as we either have upgraded to
- * WebSockets or otherwise closed the socket. */
- void *returnedUser = requestHandler(user, req);
- if (returnedUser != user) {
- /* We are upgraded to WebSocket or otherwise broken */
- return {consumedTotal, returnedUser};
- }
- // todo: do not check this for GET (get should not have a body)
- // todo: also support reading chunked streams
- std::string_view contentLengthString = req->getHeader("content-length");
- if (contentLengthString.length()) {
- remainingStreamingBytes = toUnsignedInteger(contentLengthString);
- if (!CONSUME_MINIMALLY) {
- unsigned int emittable = std::min<unsigned int>(remainingStreamingBytes, length);
- dataHandler(user, std::string_view(data, emittable), emittable == remainingStreamingBytes);
- remainingStreamingBytes -= emittable;
- data += emittable;
- length -= emittable;
- consumedTotal += emittable;
- }
- } else {
- /* Still emit an empty data chunk to signal no data */
- dataHandler(user, {}, true);
- }
- if (CONSUME_MINIMALLY) {
- break;
- }
- }
- return {consumedTotal, user};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement