Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ff7b402
Showing
8 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
#include "handler.h" | ||
|
||
void handle_request(int sockfd, const char *request) | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void handle_request(int sockfd, const char *request); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |