Files
c-prime/application/source/examples/example.web.server.c
2025-11-17 10:28:09 +01:00

70 lines
1.1 KiB
C

#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 );
}