server-socket-flex>
topical media & game development
topical media & game development
? /
server-socket-flex.c
include <hush /source.h>
include "socket/webserver.h"
include <hush /iostream.h>
//#include <Socket.h>
static int count = 0;
void Get(webserver::http_request* r) {
// Socket s = *(r->s_);
count++;
cerr << "request[" << count << "]:" << r->path_ << endl;
if (count <= 0) {
r->answer_ = "<?xml version=\"1.0\"?>\
<!DOCTYPE cross-domain-policy\
SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\
<cross -domain-policy\
xmlns:xsi=\”www.w3.org/2001/XMLSchema-instanc”\
<allow -access-from domain=\"*" to-ports=\"*\"/>\
</cross -domain-policy>";
cerr << r->answer_ << endl;
} else {
std::string title;
std::string body;
std::string bgcolor="#ffffff";
std::string links =
"<br> red</a> "
"<br> blue</a> "
"<br> form</a> "
"<br> authentication example</a> [use <b> adp</b> as username and <b> gmbh</b> as password"
"<br> show some HTTP header details</a> "
;
server
if(r->path_ == "/") {
title = "Web Server Example";
body = "<h 1>Welcome to Rene's Web Server</h 1>"
"I wonder what you're going to click" + links;
}
else if (r->path_ == "/red") {
bgcolor = "#ff4444";
title = "You chose red";
body = "<h 1>Red</h 1>" + links;
}
else if (r->path_ == "/blue") {
bgcolor = "#4444ff";
title = "You chose blue";
body = "<h 1>Blue</h 1>" + links;
}
else if (r->path_ == "/form") {
title = "Fill a form";
body = "<h 1>Fill a form</h 1>";
body += "<form action='/form'>"
"<table> "
"<tr> <td> Field 1</td> <td> <input name=field_1></td> </tr> "
"<tr> <td> Field 2</td> <td> <input name=field_2></td> </tr> "
"<tr> <td> Field 3</td> <td> <input name=field_3></td> </tr> "
"</table> "
"<input type=submit></form> ";
for (std::map<std ::string, std::string>::const_iterator i = r->params_.begin();
i != r->params_.end();
i++) {
body += "<br> " + i->first + " = " + i->second;
}
body += "<br> " + links;
}
else if (r->path_ == "/auth") {
if (r->authentication_given_) {
if (r->username_ == "adp" && r->password_ == "gmbh") {
body = "<h 1>Successfully authenticated</h 1>" + links;
}
else {
body = "<h 1>Wrong username or password</h 1>" + links;
r->auth_realm_ = "Private Stuff";
}
}
else {
r->auth_realm_ = "Private Stuff";
}
}
else if (r->path_ == "/header") {
title = "some HTTP header details";
body = std::string ("<table> ") +
"<tr> <td> Accept:</td> <td> " + r->accept_ + "</td> </tr> " +
"<tr> <td> Accept-Encoding:</td> <td> " + r->accept_encoding_ + "</td> </tr> " +
"<tr> <td> Accept-Language:</td> <td> " + r->accept_language_ + "</td> </tr> " +
"<tr> <td> User-Agent:</td> <td> " + r->user_agent_ + "</td> </tr> " +
"</table> " +
links;
}
else {
r->status_ = "404 Not Found";
title = "Wrong URL";
body = "<h 1>Wrong URL</h 1>";
body += "Path is : >" + r->path_ + "<";
}
r->answer_ = "<html> <head> <title> ";
r->answer_ += title;
r->answer_ += "</title> </head> <body bgcolor='" + bgcolor + "'>";
r->answer_ += body;
r->answer_ += "</body> </html> ";
}
}
int main() {
webserver(9001, Get);
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.