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

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

Moved sendfile to protocol.cfa to reduce compilation time

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