source: benchmark/io/http/protocol.cfa@ 426f60c

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 426f60c was ba77750, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Missing commits for plaintext

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