mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 22:02:49 -06:00
reference: HttpServer example
git-svn-id: svn://ultimatepp.org/upp/trunk@4990 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
8dc2949f55
commit
3156ff7d87
3 changed files with 64 additions and 0 deletions
50
reference/HttpServer/HttpServer.cpp
Normal file
50
reference/HttpServer/HttpServer.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
TcpSocket server;
|
||||
StaticMutex ServerMutex;
|
||||
|
||||
void Server()
|
||||
{
|
||||
for(;;) {
|
||||
TcpSocket socket;
|
||||
LOG("Waiting...");
|
||||
ServerMutex.Enter();
|
||||
bool b = socket.Accept(server);
|
||||
ServerMutex.Leave();
|
||||
if(b) {
|
||||
ServerMutex.Leave();
|
||||
LOG("Connection accepted");
|
||||
HttpHeader http;
|
||||
http.Read(socket);
|
||||
String html;
|
||||
html << "<html>"
|
||||
<< "<b>Method:</b> " << http.GetMethod() << "<br>"
|
||||
<< "<b>URI:</b> " << http.GetURI() << "<br>";
|
||||
for(int i = 0; i < http.fields.GetCount(); i++)
|
||||
html << "<b>" << http.fields.GetKey(i) << ":</b> " << http.fields[i] << "<br>";
|
||||
int len = http.GetContentLength();
|
||||
if(len > 0)
|
||||
socket.GetAll(len);
|
||||
html << "<b><i>Current time:</i></b> " << GetSysTime() << "</html>";
|
||||
HttpResponse(socket, http.scgi, 200, "OK", "text/html", html);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
StdLogSetup(LOG_COUT|LOG_FILE);
|
||||
|
||||
if(!server.Listen(4000, 10)) {
|
||||
LOG("Cannot open server port for listening\r\n");
|
||||
return;
|
||||
}
|
||||
#ifdef _MULTITHREADED
|
||||
const int NTHREADS = 10;
|
||||
for(int i = 0; i < NTHREADS; i++)
|
||||
Thread::Start(callback(Server));
|
||||
#endif
|
||||
Server();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue