Roughly implement ideas

This commit is contained in:
Alex Hultman
2018-06-18 22:53:58 +02:00
parent 4721b47743
commit 2c2fe0f56f
5 changed files with 72 additions and 54 deletions
+7 -44
View File
@@ -1,59 +1,22 @@
// uWS.h
#include "Hub.h"
// this is roughly the interfaces I plan for (with changes and additional helpers added with time)
// much speaks for a header-only or header-mostly implementation now that uSockets is properly isolating its internal headers
char largeBuf[] = "HTTP/1.1 200 OK\r\nContent-Length: 512\r\n\r\n";
int largeHttpBufSize = sizeof(largeBuf) + 512 - 1;
char *largeHttpBuf;
#include "uWS.h"
int main() {
largeHttpBuf = (char *) malloc(largeHttpBufSize);
memcpy(largeHttpBuf, largeBuf, sizeof(largeBuf) - 1);
std::cout << "HttpSocket size: " << sizeof(HttpSocket) << std::endl;
std::cout << "HttpSocket size: " << sizeof(HttpSocket::Data) << std::endl;
char *buffer = new char[512];
// either use this, or use onHttpRoute but not both
uWS::defaultHub.onHttpRequest([](HttpSocket *s, HttpRequest *req) {
uWS::defaultHub.onHttpRequest([buffer](HttpSocket *s, HttpRequest *req) {
// we do not care for routers just yet!
if (req->getUrl() == "/") {
// just write back for now!
us_socket *us = (us_socket *) s;
us_socket_write(us, largeHttpBuf, largeHttpBufSize, 0);
s->writeStatus(200)->writeHeader("Server", "µWebSockets v0.15")->end(buffer, 512);
} else {
std::cout << "Got HTTP request at URL: " << req->getUrl() << std::endl;
}
// get the URL here!
// an http socket can BOTH send buffered data AND have one stream in and one stream out!
// writeStatus(234314234)
// writeHeader(key, value)
// streamOut(asdasdasdsad)
// if some asshole posted data, stream it in
/*s->streamIn([]() {
});*/
//std::cout << "Got data: " << std::string(data, length) << std::endl;
// s->writeStatus();
// s->writeHeader();
// if url == / then stream index.htm
// stream out the response
/*s->stream(largeHttpBufSize, [](int offset) {
return std::make_pair(largeHttpBuf + offset, largeHttpBufSize - offset);
});*/
}).listen("localhost", 3000, 0).run();
// todo: important swapping to SSL should be .secureListen(same interfaces) and work out of the box!