summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Christian Pohle2021-11-10 20:38:34 +0100
committerMax Christian Pohle2021-11-10 20:38:34 +0100
commit41c1284bd4bc791a65a2e07faeaec82f5dd4066c (patch)
tree9e39643666da13dc027c1040733cccac17b9fdd1
parent829dcecb85edbc815dcb009083221cb6a05ddd8a (diff)
downloadohmycgi-41c1284bd4bc791a65a2e07faeaec82f5dd4066c.tar.bz2
ohmycgi-41c1284bd4bc791a65a2e07faeaec82f5dd4066c.zip
http answer implemented
-rw-r--r--main.c73
1 files changed, 63 insertions, 10 deletions
diff --git a/main.c b/main.c
index 18e33f5..b8be592 100644
--- a/main.c
+++ b/main.c
@@ -3,6 +3,8 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include <string.h> 4#include <string.h>
5#include <sys/socket.h> 5#include <sys/socket.h>
6#include <sys/epoll.h>
7#include <fcntl.h>
6#include <unistd.h> 8#include <unistd.h>
7 9
8#include <err.h> 10#include <err.h>
@@ -10,16 +12,63 @@
10 12
11#include <pthread.h> 13#include <pthread.h>
12 14
13void * next_customer(int new_socket) 15void * next_customer(void * new_socket)
14{ 16{
15 char *hello = "Hello from server"; 17 if (new_socket == 0) return NULL;
16 char buffer[1024] = {0};
17 int valread = read(new_socket, buffer, 1024);
18 printf("%s %d\n", buffer, valread);
19 18
20 send(new_socket, hello, strlen(hello), 0);
21 19
22 printf("Hello message sent\n"); 20 // read the input data...
21 char * input_buffer = {0};
22 size_t input_buffer_position = 0;
23 FILE * f_input = open_memstream(&input_buffer, &input_buffer_position);
24
25
26 for(
27 int size=1024,
28 read_buffer[1024] = {0};
29 size>0;
30 size=recv((size_t) new_socket, read_buffer, 1024, MSG_WAITALL)) {
31 fwrite(read_buffer, size, 1, f_input);
32 usleep(10); // TODO: implement with some sort of epoll
33 }
34
35 fflush(f_input);
36 printf("input_buffer has %ld bytes and contains:\n", input_buffer_position);
37
38 char * c = strstr(input_buffer, "Content-Type");
39 printf("WE ARE HERE: %s\n", c);
40 write(STDOUT_FILENO, input_buffer, input_buffer_position);
41
42
43
44 // answer the call...
45
46 char * buffer = {0};
47 size_t buffer_position = 0;
48 FILE * f = open_memstream(&buffer, &buffer_position);
49 fputs("HTTP/1.0 200 OK\n", f);
50 fputs("content-type: text/html\n\n", f);
51 fputs("<html>", f);
52 fputs("<head>", f);
53 fputs("<title>test</title>", f);
54 fputs("<style>label { display:block }</style>", f);
55 fputs("</head>", f);
56 fputs("<form action=\"/\" method=\"post\" enctype=\"multipart/form-data\">", f);
57 fputs("<label>Line1<br/>\n", f);
58 fputs("<textarea type=\"text\" name=\"text\"></textarea>", f);
59 fputs("</label><br/>\n", f);
60 fputs("<input type=\"submit\" name=\"okay\" value=\"ok\" />", f);
61 fputs("</form>", f);
62 fputs("</html>\n", f);
63 fflush(f);
64 printf("OOOOO [%ld] %s\n", buffer_position, buffer);
65
66 send((size_t) new_socket, buffer, buffer_position, MSG_EOR);
67 // fclose(f_socket);
68 close((size_t) new_socket);
69 fclose(f_input);
70 fclose(f);
71
23 return NULL; 72 return NULL;
24} 73}
25 74
@@ -29,13 +78,17 @@ void serve(int server_fd)
29 int addrlen = sizeof(address); 78 int addrlen = sizeof(address);
30 warn("waiting for connections on %d", server_fd); 79 warn("waiting for connections on %d", server_fd);
31 80
32 for(int new_socket = 0 81 for(size_t new_socket = 0
33 ; 1 ; new_socket=accept(server_fd, (struct sockaddr*) &address, (socklen_t*) &addrlen)) 82 ; 1 ; new_socket=accept(server_fd, (struct sockaddr*) &address, (socklen_t*) &addrlen))
34 { 83 {
35 warn("next: %d", new_socket); 84 warn("next: %ld\n", new_socket);
85 // set non blocking mode...
86 fcntl((size_t) new_socket, F_SETFL,
87 fcntl((size_t) new_socket, F_GETFL) | O_NONBLOCK);
88
36 // next_customer(new_socket); 89 // next_customer(new_socket);
37 pthread_t thread_id; 90 pthread_t thread_id;
38 pthread_create(&thread_id, NULL, next_customer, new_socket); 91 pthread_create(&thread_id, NULL, next_customer, (void*) new_socket);
39 pthread_join(thread_id, NULL); 92 pthread_join(thread_id, NULL);
40 } 93 }
41} 94}
..