Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderanger committed Mar 9, 2011
0 parents commit ff7b402
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
9 changes: 9 additions & 0 deletions handler.c
@@ -0,0 +1,9 @@
#include <string.h>
#include <stdio.h>

#include "handler.h"

void handle_request(int sockfd, const char *request)
{

}
3 changes: 3 additions & 0 deletions handler.h
@@ -0,0 +1,3 @@
#pragma once

void handle_request(int sockfd, const char *request);
Binary file added img/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions index.html
@@ -0,0 +1,13 @@
<html>
<head>
<title>A webpage</title>
</head>
<body>
<h1>Hello world</h1>
<img src="/img/logo.png" />
<form method="post" action="/test.py">
<input name="name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
49 changes: 49 additions & 0 deletions main.c
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "handler.h"

#define HTTP_PORT 8080
#define REQUEST_BUFFER_SIZE (1024*1024)

void die(const char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int listenfd, sockfd;
struct sockaddr_in listen_addr = {0};
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
die("Unable to open socket");
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(HTTP_PORT);
if (bind(listenfd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0)
die("Unable to bind()");
listen(listenfd, 32);
while(1)
{
struct sockaddr_in client_addr = {0};
socklen_t addr_len;
char *request = malloc(REQUEST_BUFFER_SIZE);
addr_len = sizeof(client_addr);
sockfd = accept(listenfd, (struct sockaddr *) &client_addr, &addr_len);
if (sockfd < 0)
die("Unable to accept new connection");
read(sockfd, request, REQUEST_BUFFER_SIZE);
handle_request(sockfd, request);
close(sockfd);
free(request);
}
close(listenfd);
return 0;
}
48 changes: 48 additions & 0 deletions subprocess.c
@@ -0,0 +1,48 @@
#include "subprocess.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int subprocess(const char *path, const char *in_data, size_t in_len, char **out_data, size_t *out_len)
{
int pipe_stdin[2], pipe_stdout[2];
pid_t pid;

pipe(pipe_stdin);
pipe(pipe_stdout);

pid = fork();
if(pid)
{
int ret_val;
size_t len = 0, size=1024, n;
char *out = malloc(1024);
close(pipe_stdin[0]);
close(pipe_stdout[1]);
write(pipe_stdin[1], in_data, in_len);
close(pipe_stdin[1]);
waitpid(pid, &ret_val, 0);
while((n = read(pipe_stdout[0], out+len, size-len)) > 0)
{
len += n;
if(len == size)
{
size *= 10;
realloc(out, size);
}
}
close(pipe_stdout[0]);
if(out_data) *out_data = out;
if(out_len) *out_len = len;
return ret_val;
}
else
{
close(pipe_stdin[1]);
close(pipe_stdout[0]);
dup2(pipe_stdin[0], 0);
dup2(pipe_stdout[1], 1);
execl(path, path, NULL);
exit(404);
}
}
4 changes: 4 additions & 0 deletions subprocess.h
@@ -0,0 +1,4 @@
#pragma once
#include <stddef.h>

int subprocess(const char *path, const char *in_data, size_t in_len, char **out_data, size_t *out_len);
6 changes: 6 additions & 0 deletions test.py
@@ -0,0 +1,6 @@
#!/usr/bin/env python
import cgi
import sys

params = cgi.parse_qs(sys.stdin.read())
print 'Hello %s!'%(params['name'][0])

0 comments on commit ff7b402

Please sign in to comment.