source: benchmark/io/http/protocol.cfa@ d23c0b2

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

httpforall now only loads file if explicit path is given.

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