-
|
I am trying to port the code from v0.14 to v0.16. Back in v.014 there is a lot of freedom for the user to do low-level writes like res->write("\r\n\r\n", 4); or // chunk contents The new version v0.16 does not seem to allow one to do write(const char* msg, int len), there is only write(std::string_view data). Trying to do call Super::write(const char* msg, int len) from a user code results in errors like "error: ‘Super’ has not been declared" or Super::write is protected etc. Is there not an easy way to do low-level things anymore? I have a lot of custom low-level functions that need to be upgraded to v0.16, i.e. void write_content_length(uWS::HttpResponse *res, size_t length) { Simply replacing "uWS::HttpResponse *res" with "auto *res" is not sufficient. The API seems to have changed quite a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
If you want to stream in chunks you just call write and it will automatically go into chunking mode. It works exactly like Node.js. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Alex, so you mean there is no need to do "write_key_value(stream->res, "Transfer-Encoding", "chunked");", and then write out the chunk length plus "stream->res->write("\r\n", 2);" as a chunk separator? One can just write the chunk body with write(std::string_view data)? And the last chunk should be sent with the function "end(std::string_view data = {})", is that right? What about using the old function (now Super::)write(const char* msg, int len)? It does not seem possible to use it anymore. One can only use write(std::string_view). In the new v0.16 version, do html responses always need to be ended with a command "end()" instead of a manual "res->write("\r\n\r\n", 4);"? With the old but stable v0.14 I used to send data manually the following way, including adding the content length, the OK status etc: write_status(res, 200, "OK"); |
Beta Was this translation helpful? Give feedback.
-
|
If you take a look at the TypeScript docs over at uWebSockets.js you can see the new helper functions. You don't need to do this manullay, there are helpers for this now. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, indeed there are helper functions for writing the header, status etc. The upgrade to v0.16 (a multithreaded HTTP+WS server) ended abruptly with the following error: Only the first HTTP request (a file) gets served properly. Upon serving subsequent files the program crashes with a segmentation fault. The code used to work perfectly well with v0.14. The serve_file function is per the following. Could it be that the std::string_view body simply does not live long enough, does v0.16 not copy data in the end(std::string_view) function? void serve_file(auto *res, std::string uri) { // strip '?' from the requested file name if (pos != std::string::npos) std::cout << "serving " << resource << std::endl; // mmap a disk resource struct stat64 st; fd = open(resource.c_str(), O_RDONLY); if (fd != -1) { } else |
Beta Was this translation helpful? Give feedback.
-
|
OK. Am closing this issue. The reason for the above error message had to do with not handling responses for all possible cases. After adding a default handler for the remaining request conditions the code does not crash anymore. |
Beta Was this translation helpful? Give feedback.
If you want to stream in chunks you just call write and it will automatically go into chunking mode. It works exactly like Node.js.