Holy crap I have a working HTTP server

parent 3ca42e38
......@@ -6,6 +6,83 @@ function (args) {
var timers = Queues.timers;
var compile = modules.compile;
modules.webserver();
// modules.webserver();
var print = modules.print
var netLib = modules.netLib
var port = 8080;
var buffsize = 2000;
/* Default HTTP page with HTTP headers */
var webpage =
"HTTP/1.0 200 OK\n" +
"Server: BareMetal (http://www.returninfinity.com)\n" +
"Content-type: text/html\n" +
"\n" +
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<title>Hello world</title>\n" +
"</head>\n" +
"<body>\n" +
"Hello World!\n" +
"</body>\n" +
"</html>\n";
var webpageBuffer = Uint8Array.allocPlain(webpage);
print(webpageBuffer.length)
var buffer = Uint8Array.allocPlain(1000);
var tint = 0;
print("Web Server demo for netLib v0.4\nListening on port" + port + "\n");
// Start the netLib library
netLib.netInit();
// Initiate the sockets
var clientsocket = netLib.netInitSocket();
var listensocket = netLib.netInitSocket();
// Set the server to listen
netLib.netListen(listensocket, port);
print("\n\n");
var connections = 0
// Main program loop
while (true) {
// Check for an incoming connection
if (netLib.netAccept(listensocket, clientsocket)) {
// A client has connected!
print("\nHello.");
// Check for data to receive and print if there was something
tint = netLib.netRecv(clientsocket, buffer);
if (tint > 0) {
print("\nReceived " + tint + " bytes.");
}
print(new TextDecoder().decode(buffer));
// Send data to the client
tint = netLib.netSend(clientsocket, webpageBuffer);
print("\nSent " + tint + " bytes.");
// Disconnect the client
netLib.netDisconnect(clientsocket);
print("\nGoodbye.\n\n");
// Each thread serves 2 requests before getting tired.
if (++connections == 2) break;
}
}
print("\nThat's enough for now! Bye!\n\n");
netLib.netDisconnect(listensocket);
// Stop the netLib library
netLib.netStop();
print("\n");
return
}
#include "duktape/duktape.h"
#include "netLib/netSocket.h"
#include "bindings_netLib.h"
const int port = 8192;
const int buffsize = 2000;
void app_push_netLib(duk_context *ctx) {
duk_push_object(ctx);
duk_push_c_function(ctx, wrap_netInit, 0);
duk_put_prop_string(ctx, -2, "netInit");
duk_push_c_function(ctx, wrap_netStop, 0);
duk_put_prop_string(ctx, -2, "netStop");
duk_push_c_function(ctx, wrap_netInitSocket, 0);
duk_put_prop_string(ctx, -2, "netInitSocket");
duk_push_c_function(ctx, wrap_netConnect, 3);
duk_put_prop_string(ctx, -2, "netConnect");
duk_push_c_function(ctx, wrap_netDisconnect, 1);
duk_put_prop_string(ctx, -2, "netDisconnect");
duk_push_c_function(ctx, wrap_netListen, 2);
duk_put_prop_string(ctx, -2, "netListen");
duk_push_c_function(ctx, wrap_netAccept, 2);
duk_put_prop_string(ctx, -2, "netAccept");
duk_push_c_function(ctx, wrap_netIsDataPending, 1);
duk_put_prop_string(ctx, -2, "netIsDataPending");
duk_push_c_function(ctx, wrap_netSend, 2);
duk_put_prop_string(ctx, -2, "netSend");
duk_push_c_function(ctx, wrap_netRecv, 2);
duk_put_prop_string(ctx, -2, "netRecv");
}
duk_ret_t wrap_netSocketStruct(duk_context *ctx, netSocket *socket) {
duk_push_object(ctx);
duk_push_int(ctx, socket->number); // the socket descriptor from the socket() call
duk_put_prop_string(ctx, -2, "number");
duk_push_boolean(ctx, socket->inuse); // 1 for active, 0 for not active
duk_put_prop_string(ctx, -2, "inuse");
duk_push_int(ctx, socket->retval); // all functions will update this variable with the function return value
duk_put_prop_string(ctx, -2, "retval");
// TODO: push struct timeval timeout; // timeout value
// TODO: push struct sockaddr_in addr; // connector's address information
// struct sockaddr_in {
// sa_family_t sin_family; /* address family: AF_INET */
// in_port_t sin_port; /* port in network byte order */
// struct in_addr sin_addr; /* internet address */
// };
//
// /* Internet address. */
// struct in_addr {
// uint32_t s_addr; /* address in network byte order */
// };
return 1;
}
duk_ret_t wrap_netInit(duk_context *ctx) {
// Start the netLib library
if (netInit() == -1) {
return duk_generic_error(ctx, "netInit error");
}
return 0;
}
duk_ret_t wrap_netStop(duk_context *ctx) {
// Start the netLib library
if (netStop() == -1) {
return duk_generic_error(ctx, "netStop error");
}
return 0;
}
duk_ret_t wrap_netInitSocket(duk_context *ctx) {
netSocket *socket;
// Initiate the sockets
socket = netInitSocket();
duk_push_pointer(ctx, (void *) socket);
// wrap_netSocketStruct(ctx, socket);
return 1;
}
duk_ret_t wrap_netConnect(duk_context *ctx) {
netSocket *netSock = duk_get_pointer(ctx, 0);
const char *address = duk_get_string(ctx, 1);
unsigned short port = duk_get_int(ctx, 2);
if (netConnect(netSock, address, port) == -1) {
return duk_generic_error(ctx, "netConnect error");
}
return 0;
}
duk_ret_t wrap_netDisconnect(duk_context *ctx) {
netSocket *netSock = duk_get_pointer(ctx, 0);
if (netDisconnect(netSock) == -1) {
return duk_generic_error(ctx, "netDisconnect error");
}
return 0;
}
duk_ret_t wrap_netListen(duk_context *ctx) {
netSocket *listensocket = duk_get_pointer(ctx, -2);
unsigned short port = duk_get_int(ctx, -1);
if (netListen(listensocket, port) == -1) {
return duk_generic_error(ctx, "netListen error");
}
return 0;
}
duk_ret_t wrap_netAccept(duk_context *ctx) {
netSocket *listenSock = duk_get_pointer(ctx, -2);
netSocket *netSock = duk_get_pointer(ctx, -1);
int ret = netAccept(listenSock, netSock);
if (ret == -1) {
return duk_generic_error(ctx, "netAccept error");
}
duk_push_boolean(ctx, ret);
return 1;
}
duk_ret_t wrap_netIsDataPending(duk_context *ctx) {
netSocket *netSock = duk_get_pointer(ctx, -1);
int ret = netIsDataPending(netSock);
duk_pop(ctx);
duk_push_boolean(ctx, ret);
return 1;
}
duk_ret_t wrap_netSend(duk_context *ctx) {
netSocket *netSock = duk_get_pointer(ctx, 0);
duk_size_t bytes = 0;
const char *data = duk_get_buffer(ctx, 1, &bytes);
int ret = netSend(netSock, data, (int) bytes);
printf("bytes = %d, ret=%d", bytes, ret);
if (ret == -1) {
return duk_generic_error(ctx, "netSend error");
}
duk_pop_2(ctx);
duk_push_int(ctx, ret);
return 1;
}
duk_ret_t wrap_netRecv(duk_context *ctx) {
netSocket *netSock = duk_get_pointer(ctx, 0);
duk_size_t bytes = 0;
char *data = duk_get_buffer(ctx, 1, &bytes);
int ret = netRecv(netSock, data, (int) bytes);
if (ret == -1) {
return duk_generic_error(ctx, "netRecv error");
}
duk_pop_2(ctx);
duk_push_int(ctx, ret);
printf("bytes = %d, ret=%d", bytes, ret);
return 1;
}
// TODO: netSetTimeout
// TODO: netSetOption
//
......@@ -78,8 +78,8 @@ int main(int argc, char *argv[] /* char *environ[] */) {
duk_put_prop_string(ctx, -2, "assert_safe_path");
duk_push_c_function(ctx, native_sleep, 1);
duk_put_prop_string(ctx, -2, "sleep");
duk_push_c_function(ctx, native_socket, 0);
duk_put_prop_string(ctx, -2, "webserver");
app_push_netLib(ctx);
duk_put_prop_string(ctx, -2, "netLib");
duk_put_prop_string(ctx, -2, "modules");
// Provide direct access to the event loop
duk_push_object(ctx);
......
#include "duktape/duktape.h"
#include "native_print.h"
#include "netLib/netSocket.h"
// Working command in real Windows (not Windows Subsystem for Linux):
// gcc webserver.c netSocket.c -o webserver -DMS_WINDOWS -lwsock32
const int port = 8192;
const int buffsize = 2000;
/* Default HTTP page with HTTP headers */
char webpage[] =
"HTTP/1.0 200 OK\n"
"Server: BareMetal (http://www.returninfinity.com)\n"
"Content-type: text/html\n"
"\n"
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<title>Hello world</title>\n"
"</head>\n"
"<body>\n"
"Hello World!\n"
"</body>\n"
"</html>\n";
duk_ret_t native_socket(duk_context *ctx) {
int tint;
netSocket *clientsocket, *listensocket;
char tempbuffer[buffsize];
printf("Web Server demo for netLib v0.4\nListening on port %d\n", port);
// Start the netLib library
netInit();
// Initiate the sockets
clientsocket = netInitSocket();
listensocket = netInitSocket();
// Set the server to listen
netListen(listensocket, port);
printf("\n\n");
// Main program loop
for (;;)
{
// Check for an incoming connection
if (netAccept(listensocket, clientsocket) > 0)
{
// A client has connected!
printf("\nHello.");
// Clear the buffer
memset(tempbuffer, 0, buffsize);
// Check for data to receive and print if there was something
tint = netRecv(clientsocket, tempbuffer, buffsize);
if (tint > 0)
{
printf("\nReceived %d bytes.", tint);
// printf("\n--------------------\n%s", tempbuffer);
}
// Send data to the client
tint = netSend(clientsocket, webpage, strlen(webpage));
printf("\nSent %d bytes.", tint);
// Disconnect the client
netDisconnect(clientsocket);
printf("\nGoodbye.\n\n");
}
// Why isn't this done on its own...
fflush(stdout);
}
netDisconnect(listensocket);
// Stop the netLib library
netStop();
printf("\n");
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment