source: benchmark/io/http/protocol.cfa @ 6a531ab

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

Fixed some compilation errors.
Fixed file descriptor support in progress.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#include "protocol.hfa"
2
3#define _GNU_SOURCE
4extern "C" {
5        #include <fcntl.h>
6}
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, ... );
13        #include <linux/io_uring.h>
14}
15#include <string.h>
16#include <errno.h>
17
18#include "options.hfa"
19
20const char * http_msgs[] = {
21        "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: %zu\n\n",
22        "HTTP/1.1 400 Bad Request\nContent-Type: text/plain\nContent-Length: 0\n\n",
23        "HTTP/1.1 404 Not Found\nContent-Type: text/plain\nContent-Length: 0\n\n",
24        "HTTP/1.1 413 Payload Too Large\nContent-Type: text/plain\nContent-Length: 0\n\n",
25        "HTTP/1.1 414 URI Too Long\nContent-Type: text/plain\nContent-Length: 0\n\n",
26};
27
28_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
29
30const int http_codes[] = {
31        200,
32        400,
33        404,
34        413,
35        414,
36};
37
38_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
39
40int code_val(HttpCode code) {
41        return http_codes[code];
42}
43
44static inline int answer( int fd, const char * it, int len) {
45        while(len > 0) {
46                // Call write
47                int ret = write(fd, it, len);
48                if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); }
49
50                // update it/len
51                it  += ret;
52                len -= ret;
53        }
54        return 0;
55}
56
57int answer_error( int fd, HttpCode code ) {
58        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
59        int idx = (int)code;
60        return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
61}
62
63int answer_header( int fd, size_t size ) {
64        const char * fmt = http_msgs[OK200];
65        int len = 100;
66        char buffer[len];
67        len = snprintf(buffer, len, fmt, size);
68        return answer( fd, buffer, len );
69}
70
71[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
72        char * it = buffer;
73        size_t count = len - 1;
74        int rlen = 0;
75        READ:
76        for() {
77                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, 0p, 0p);
78                if(ret == 0 ) return [OK200, true, 0, 0];
79                if(ret < 0 ) {
80                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
81                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
82                }
83                it[ret + 1] = '\0';
84                rlen += ret;
85
86                if( strstr( it, "\r\n\r\n" ) ) break;
87
88                it += ret;
89                count -= ret;
90
91                if( count < 1 ) return [E414, false, 0, 0];
92        }
93
94        printf("%.*s\n", rlen, buffer);
95
96        it = buffer;
97        int ret = memcmp(it, "GET /", 5);
98        if( ret != 0 ) return [E400, false, 0, 0];
99        it += 5;
100
101        char * end = strstr( it, " " );
102        return [OK200, false, it, end - it];
103}
104
105void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
106        off_t offset = 0;
107        ssize_t ret;
108        SPLICE1: while(count > 0) {
109                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
110                if( ret < 0 ) {
111                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
112                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
113                }
114
115                count -= ret;
116                offset += ret;
117                size_t in_pipe = ret;
118                SPLICE2: while(in_pipe > 0) {
119                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
120                        if( ret < 0 ) {
121                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
122                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
123                        }
124                        in_pipe -= ret;
125                }
126
127        }
128}
Note: See TracBrowser for help on using the repository browser.