Bind to JavaScriptCore. Serve files through embedded web server.

This commit is contained in:
Matthew Bowra-Dean
2010-11-25 00:11:29 +13:00
committed by Chris Forbes
parent a561dd376e
commit 69ee1399b9
3 changed files with 87 additions and 104 deletions

View File

@@ -6,12 +6,13 @@
* see LICENSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#define MHD_PLATFORM_H
#include <microhttpd.h>
@@ -26,6 +27,27 @@ int http_get_args(void * cls, enum MHD_ValueKind kind,
return MHD_YES;
}
int try_file_response(const char * url, struct MHD_Connection * connection)
{
int fd, ret;
struct MHD_Response * response;
struct stat sbuf;
g_message("Opening %s", url + 1);
if ((-1 == (fd = open(url + 1, O_RDONLY))) ||
(0 != fstat(fd, &sbuf)))
{
return MHD_NO;
}
response = MHD_create_response_from_fd(sbuf.st_size, fd);
MHD_add_response_header(response, "Content-Type", "text/html");
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int access_handler_callback(void * userdata,
struct MHD_Connection * connection,
const char * url,
@@ -36,19 +58,21 @@ int access_handler_callback(void * userdata,
void ** userpointer)
{
struct MHD_Response * response;
int ret;
int ret = MHD_NO;
char * text = "1";
g_message(url);
if ((ret = try_file_response(url, connection)))
return ret;
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
&http_get_args, NULL);
text = "<html><head><title>Not found</title></head><body>File not found</body></html>";
response = MHD_create_response_from_data(strlen(text), (void *)text,
MHD_NO, MHD_NO);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response(response);
return ret;
}