sourcey / libsourcey

C++14 evented IO libraries for high performance networking and media based applications
https://sourcey.com/libsourcey
GNU Lesser General Public License v2.1
1.31k stars 347 forks source link

HTTP Echo server doesn't respond! #219

Closed azmathmoosa closed 6 years ago

azmathmoosa commented 6 years ago

I am trying to write a simple http server with libsourcey but I have failed so far.

This code compiles and runs but the server at 1337 doesn't respond!

unsigned int port = 1337;
net::Address localhost("0.0.0.0", port);        
http::Server srv(localhost);       
srv.Connection += [](http::ServerConnection::Ptr conn) {
        conn->Payload += [](http::ServerConnection& conn, const MutableBuffer& buffer) {
            conn.send(bufferCast<const char*>(buffer), buffer.size());
            conn.close();
        };
    };
srv.start();
htlog << LINFO << "HTTP Serving on port: " << port;
htlog << LINFO << "Server root: " << basepath;
htlog << LINFO << "======SERVER READY======";
cout << "Ctrl+C to kill" << endl;
waitForShutdown();

This is the wrk output

wrk -d2s --timeout 2s http://localhost:1337 Running 2s test @ http://localhost:1337 2 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev Latency 0.00us 0.00us 0.00us -nan% Req/Sec 0.00 0.00 0.00 -nan% 0 requests in 2.00s, 0.00B read Requests/sec: 0.00 Transfer/sec: 0.00B

localhost:1337 is available, but is sending nothing in return. Please help. I would also like to know how I can implement routing and stuff.

azmathmoosa commented 6 years ago

this worked

conn->response().add("Content-Length", "0");
        conn->response().add("Connection", "close"); // "keep-alive"
        conn->sendHeader();

but there is still no documentation for File uploads, POST parsing etc. Can you guide me pls?

auscaster commented 6 years ago

Sorry about the lack of examples for the HTTP implementation! The best source is the tests file: https://github.com/sourcey/libsourcey/blob/master/src/http/tests/httptests.cpp

The HTTP module is used extensively throughout libsourcey so you can also do a code search for http:: in order to get find code samples.

Hope this helps!

On 9 February 2018 at 07:24, azmathmoosa notifications@github.com wrote:

this worked

conn->response().add("Content-Length", "0"); conn->response().add("Connection", "close"); // "keep-alive" conn->sendHeader();

but there is still no documentation for File uploads, POST parsing etc. Can you guide me pls?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sourcey/libsourcey/issues/219#issuecomment-364346594, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGKDI15dZy3DFlC3ACpYQlUo3B4QBR9ks5tS-SNgaJpZM4R93cC .

azmathmoosa commented 6 years ago

Thanks, I managed to get upto this

           srv.Connection += [&](http::ServerConnection::Ptr conn) {
            //string endpoint = resolve_api_endpoint(conn);
            NVCollection flagParams;            
            conn->request().getURIParameters(flagParams);            
            for(auto it=flagParams.begin(); it!=flagParams.end(); it++){
                cout << it->first << ":" << it->second << endl;
            }

            conn->Payload += [](http::ServerConnection& conn, const MutableBuffer& buffer) {
               /// need help here
                std::cout << "On payload: " << buffer.size() << endl;
            };
            conn->response().add("Content-Length", "0");
            conn->response().add("Connection", "close"); // "keep-alive"
            conn->sendHeader();
        };

I get this

0:inferImage returnFaceAttributes:true On payload: 65252 On payload: 65536 On payload: 65536 On payload: 34268

I wanted to know if there is an inbuilt multipart-file data parser for ServerConnection. I want my server to handle image uploads. Basically, some class that will automatically combine all the parts and return the buffer which i will then pass on to my image decoder. @auscaster Do I need to do this on my own? or is there a class for this.
Thanks.

auscaster commented 6 years ago

Please use the FormWriter class for this: https://github.com/sourcey/libsourcey/blob/master/src/http/src/form.cpp#L38