source: benchmark/io/http/protocol.cfa @ 390fb02

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 390fb02 was 390fb02, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

http server now has date and server name in response

  • Property mode set to 100644
File size: 3.5 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
[390fb02]20const char * volatile date = "Wed, 17 Apr 2013 12:00:00 GMT";
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
49                int ret = write(fd, it, len);
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
73[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
74        char * it = buffer;
75        size_t count = len - 1;
76        int rlen = 0;
77        READ:
78        for() {
[d11d6eb]79                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, 0p, 0p);
80                if(ret == 0 ) return [OK200, true, 0, 0];
[0aec496]81                if(ret < 0 ) {
82                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
83                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
84                }
85                it[ret + 1] = '\0';
86                rlen += ret;
87
88                if( strstr( it, "\r\n\r\n" ) ) break;
89
90                it += ret;
91                count -= ret;
92
[d11d6eb]93                if( count < 1 ) return [E414, false, 0, 0];
[0aec496]94        }
95
96        printf("%.*s\n", rlen, buffer);
97
98        it = buffer;
99        int ret = memcmp(it, "GET /", 5);
[d11d6eb]100        if( ret != 0 ) return [E400, false, 0, 0];
[0aec496]101        it += 5;
102
103        char * end = strstr( it, " " );
104        return [OK200, false, it, end - it];
[c82af9f]105}
106
107void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
108        off_t offset = 0;
109        ssize_t ret;
110        SPLICE1: while(count > 0) {
[d11d6eb]111                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
[c82af9f]112                if( ret < 0 ) {
113                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
114                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
115                }
116
117                count -= ret;
118                offset += ret;
119                size_t in_pipe = ret;
120                SPLICE2: while(in_pipe > 0) {
[d11d6eb]121                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
[c82af9f]122                        if( ret < 0 ) {
123                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
124                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
125                        }
126                        in_pipe -= ret;
127                }
128
129        }
[0aec496]130}
Note: See TracBrowser for help on using the repository browser.