#include "tnplib.h"
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
inline void D(const char *errormessage) {
#ifdef _DEBUG
cerr << errormessage << endl;
#endif
}
void SimpleHTTPServer(int port) {
char buffer[256];
int status;
SOCKET serve = TCPStartServer(port, 10);
for (;;) {
SOCKET visitor = TCPWaitForConnection(serve);
status = TCPRecvLine(visitor, buffer, sizeof(buffer) );
if (status<0) continue;
D(buffer);
TCPSendLine(visitor, "HTTP/1.1 200 Ok\r\n");
TCPSendLine(visitor, "Server: TNP HTTP Echo Server\r\n");
TCPSendLine(visitor, "Connection: close\r\n");
TCPSendLine(visitor, "Cache-Control: no-cache\r\nExpires: 0\r\nETag: \"\"\r\n");
TCPSendLine(visitor, "Content-Type: text/html; charset=ISO-8859-1\r\n");
TCPSendLine(visitor, "\r\n");
TCPSendLine(visitor, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\r\n");
TCPSendLine(visitor, "<meta http-equiv=\"Content-type\" content=\"text/html; charset=iso-8859-1\">\r\n");
TCPSendLine(visitor, "<html>\r\n<head><title>A simple dynamic Web page</title></head>\r\n<body>\r\n");
TCPSendLine(visitor, "<pre>");
TCPSendLine(visitor, buffer);
TCPSendLine(visitor, "\r\n");
for (;;) {
status = TCPRecvLine(visitor, buffer, sizeof(buffer) );
if (status<0) break;
if (strlen(buffer)==0) break;
TCPSendLine(visitor, buffer);
TCPSendLine(visitor, "\r\n");
}
TCPSendLine(visitor, "</pre>\r\n");
TCPSendLine(visitor, "</body>\r\n</html>\r\n");
TCPPrepClose(visitor);
TCPStopClient(visitor);
D("Done");
}
}
int main()
{
SocketLibStart();
SimpleHTTPServer(8088);
SocketLibEnd();
return(0);
}