source: benchmark/io/http/protocol.cfa@ 4c4d854

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 4c4d854 was 4f762d3, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fix httpforall after changes to I/O

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include "protocol.hfa"
2
3#define _GNU_SOURCE
4extern "C" {
5 #include <fcntl.h>
6}
7
8#include <fstream.hfa>
9#include <iofwd.hfa>
10
11#include <assert.h>
12// #include <stdio.h> // Don't use stdio.h, too slow to compile
13extern "C" {
14 int snprintf ( char * s, size_t n, const char * format, ... );
15 // #include <linux/io_uring.h>
16}
17#include <string.h>
18#include <errno.h>
19
20#include "options.hfa"
21
22const char * volatile date = 0p;
23
24const char * http_msgs[] = {
25 "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n",
26 "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
27 "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
28 "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
29 "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
30 "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
31 "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
32};
33
34_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
35
36const int http_codes[] = {
37 200,
38 400,
39 404,
40 405,
41 408,
42 413,
43 414,
44};
45
46_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
47
48int code_val(HttpCode code) {
49 return http_codes[code];
50}
51
52static inline int answer( int fd, const char * it, int len) {
53 while(len > 0) {
54 // Call write
55 int ret = cfa_send(fd, it, len, 0, 0);
56 if( ret < 0 ) {
57 if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
58 if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
59
60 abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
61 }
62
63 // update it/len
64 it += ret;
65 len -= ret;
66 }
67 return 0;
68}
69
70int answer_error( int fd, HttpCode code ) {
71 /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
72 int idx = (int)code;
73 return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
74}
75
76int answer_header( int fd, size_t size ) {
77 const char * fmt = http_msgs[OK200];
78 int len = 200;
79 char buffer[len];
80 len = snprintf(buffer, len, fmt, date, size);
81 return answer( fd, buffer, len );
82}
83
84int answer_plain( int fd, char buffer[], size_t size ) {
85 int ret = answer_header(fd, size);
86 if( ret < 0 ) return ret;
87 return answer(fd, buffer, size);
88}
89
90int answer_empty( int fd ) {
91 return answer_header(fd, 0);
92}
93
94
95[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
96 char * it = buffer;
97 size_t count = len - 1;
98 int rlen = 0;
99 READ:
100 for() {
101 int ret = cfa_recv(fd, (void*)it, count, 0, 0);
102 // int ret = read(fd, (void*)it, count);
103 if(ret == 0 ) return [OK200, true, 0, 0];
104 if(ret < 0 ) {
105 if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
106 if( errno == ECONNRESET ) return [E408, true, 0, 0];
107 if( errno == EPIPE ) return [E408, true, 0, 0];
108 abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
109 }
110 it[ret + 1] = '\0';
111 rlen += ret;
112
113 if( strstr( it, "\r\n\r\n" ) ) break;
114
115 it += ret;
116 count -= ret;
117
118 if( count < 1 ) return [E414, false, 0, 0];
119 }
120
121 if( options.log ) {
122 write(sout, buffer, rlen);
123 sout | nl;
124 }
125
126 it = buffer;
127 int ret = memcmp(it, "GET /", 5);
128 if( ret != 0 ) return [E400, false, 0, 0];
129 it += 5;
130
131 char * end = strstr( it, " " );
132 return [OK200, false, it, end - it];
133}
134
135int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
136 unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
137 off_t offset = 0;
138 ssize_t ret;
139 SPLICE1: while(count > 0) {
140 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0);
141 // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
142 if( ret < 0 ) {
143 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
144 if( errno == ECONNRESET ) return -ECONNRESET;
145 if( errno == EPIPE ) return -EPIPE;
146 abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
147 }
148
149 count -= ret;
150 offset += ret;
151 size_t in_pipe = ret;
152 SPLICE2: while(in_pipe > 0) {
153 ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0);
154 // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
155 if( ret < 0 ) {
156 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
157 if( errno == ECONNRESET ) return -ECONNRESET;
158 if( errno == EPIPE ) return -EPIPE;
159 abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
160 }
161 in_pipe -= ret;
162 }
163
164 }
165 return count;
166}
167
168//=============================================================================================
169
170#include <clock.hfa>
171#include <time.hfa>
172#include <thread.hfa>
173
174struct date_buffer {
175 char buff[100];
176};
177
178thread DateFormater {
179 int idx;
180 date_buffer buffers[2];
181};
182
183void ?{}( DateFormater & this ) {
184 ((thread&)this){ "Server Date Thread", *options.clopts.instance };
185 this.idx = 0;
186 memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
187 memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
188}
189
190void main(DateFormater & this) {
191 LOOP: for() {
192 waitfor( ^?{} : this) {
193 break LOOP;
194 }
195 or else {}
196
197 Time now = getTimeNsec();
198
199 strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
200
201 char * next = this.buffers[this.idx].buff;
202 __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
203 this.idx = (this.idx + 1) % 2;
204
205 sleep(1`s);
206 }
207}
208
209//=============================================================================================
210DateFormater * the_date_formatter;
211
212void init_protocol(void) {
213 the_date_formatter = alloc();
214 (*the_date_formatter){};
215}
216
217void deinit_protocol(void) {
218 ^(*the_date_formatter){};
219 free( the_date_formatter );
220}
Note: See TracBrowser for help on using the repository browser.