Initial commit
This commit is contained in:
70
application/source/examples/example.web.server.c
Normal file
70
application/source/examples/example.web.server.c
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
#include "../http.h"
|
||||
|
||||
#include "./fileSystem.h"
|
||||
|
||||
#include "./text.h"
|
||||
|
||||
#include "./mimeTypes.h"
|
||||
|
||||
#include "./cache.h"
|
||||
|
||||
|
||||
struct cache * cacheManager;
|
||||
|
||||
|
||||
|
||||
void handleRequest( struct request * requestInstance, struct text * response ) {
|
||||
|
||||
text * filePath = new text("www/");
|
||||
|
||||
filePath += requestInstance->url;
|
||||
|
||||
|
||||
if( filesystem->isDirectory( filePath->value ) ) {
|
||||
|
||||
filePath->value += "index.html";
|
||||
|
||||
requestInstance->mimeType = "text/html";
|
||||
|
||||
}
|
||||
|
||||
text * content = cacheManager->getFile( filePath->value );
|
||||
|
||||
//text * content = filesystem->readFile( filePath->value, "binary" );
|
||||
|
||||
response += "HTTP/1.0 200 OK\r\n";
|
||||
|
||||
response += "Server: webserver-c\r\n";
|
||||
|
||||
response += "Content-type: " + requestInstance->mimeType + "\r\n\r\n";
|
||||
|
||||
printf("Filename: %s\n", filePath->value);
|
||||
|
||||
printf("Source: %s\n", content->value);
|
||||
|
||||
response->appendObject( content );
|
||||
|
||||
response += "\r\n";
|
||||
|
||||
|
||||
|
||||
filePath->free();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
|
||||
cacheManager = new cache();
|
||||
|
||||
|
||||
|
||||
http * serverInstance = new http();
|
||||
|
||||
serverInstance->createServer( handleRequest );
|
||||
|
||||
serverInstance->listen( 8080 );
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user