Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/io/http/protocol.cfa

    r2cd784a rb57db73  
    2020#include "options.hfa"
    2121
    22 #define PLAINTEXT_1WRITE
    23 #define PLAINTEXT_NOCOPY
    24 
    25 struct https_msg_str {
    26         char msg[512];
    27         size_t len;
    28 };
    29 
    30 const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
     22const char * volatile date = 0p;
     23
     24const char * http_msgs[] = {
     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",
     28        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     29        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     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",
     32};
    3133
    3234_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
    3335
    34 const int http_codes[KNOWN_CODES] = {
    35         200,
     36const int http_codes[] = {
    3637        200,
    3738        400,
     
    5253        while(len > 0) {
    5354                // Call write
    54                 int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
     55                int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
     56                // int ret = write(fd, it, len);
    5557                if( ret < 0 ) {
    5658                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
     
    7072        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
    7173        int idx = (int)code;
    72         return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
     74        return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
    7375}
    7476
    7577int answer_header( int fd, size_t size ) {
    76         char buffer[512];
    77         char * it = buffer;
    78         memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
    79         it += http_msgs[OK200]->len;
    80         int len = http_msgs[OK200]->len;
    81         len += snprintf(it, 512 - len, "%d \n\n", size);
     78        const char * fmt = http_msgs[OK200];
     79        int len = 200;
     80        char buffer[len];
     81        len = snprintf(buffer, len, fmt, date, size);
    8282        return answer( fd, buffer, len );
    8383}
    8484
    85 #if defined(PLAINTEXT_NOCOPY)
    86 int answer_plaintext( int fd ) {
    87         return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len + 1); // +1 cause snprintf doesn't count nullterminator
    88 }
    89 #elif defined(PLAINTEXT_1WRITE)
    90 int answer_plaintext( int fd ) {
    91         char text[] = "Hello, World!\n";
    92         char buffer[512 + sizeof(text)];
    93         char * it = buffer;
    94         memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
    95         it += http_msgs[OK200]->len;
    96         int len = http_msgs[OK200]->len;
    97         int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text));
    98         it += r;
    99         len += r;
    100         memcpy(it, text, sizeof(text));
    101         return answer(fd, buffer, len + sizeof(text));
    102 }
    103 #else
    104 int answer_plaintext( int fd ) {
    105         char text[] = "Hello, World!\n";
    106         int ret = answer_header(fd, sizeof(text));
     85int answer_plain( int fd, char buffer[], size_t size ) {
     86        int ret = answer_header(fd, size);
    10787        if( ret < 0 ) return ret;
    108         return answer(fd, text, sizeof(text));
    109 }
    110 #endif
     88        return answer(fd, buffer, size);
     89}
    11190
    11291int answer_empty( int fd ) {
     
    11594
    11695
    117 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
     96[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
    11897        char * it = buffer;
    11998        size_t count = len - 1;
     
    121100        READ:
    122101        for() {
    123                 int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
     102                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
    124103                // int ret = read(fd, (void*)it, count);
    125104                if(ret == 0 ) return [OK200, true, 0, 0];
     
    160139        ssize_t ret;
    161140        SPLICE1: while(count > 0) {
    162                 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
     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);
    163143                if( ret < 0 ) {
    164144                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
     
    172152                size_t in_pipe = ret;
    173153                SPLICE2: while(in_pipe > 0) {
    174                         ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
     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);
    175156                        if( ret < 0 ) {
    176157                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
     
    192173#include <thread.hfa>
    193174
    194 const char * original_http_msgs[] = {
    195         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
    196         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n",
    197         "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    198         "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    199         "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    200         "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    201         "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    202         "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    203 };
    204 
    205175struct date_buffer {
    206         https_msg_str strs[KNOWN_CODES];
     176        char buff[100];
    207177};
    208178
     
    213183
    214184void ?{}( DateFormater & this ) {
    215         ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
     185        ((thread&)this){ "Server Date Thread", *options.clopts.instance };
    216186        this.idx = 0;
    217         memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
    218         memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
     187        memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
     188        memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
    219189}
    220190
     
    226196                or else {}
    227197
    228 
    229                 char buff[100];
    230198                Time now = getTimeNsec();
    231                 strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
    232                 sout | "Updated date to '" | buff | "'";
    233 
    234                 for(i; KNOWN_CODES) {
    235                         size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
    236                         this.buffers[this.idx].strs[i].len = len;
    237                 }
    238 
    239                 for(i; KNOWN_CODES) {
    240                         https_msg_str * next = &this.buffers[this.idx].strs[i];
    241                         __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
    242                 }
     199
     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);
    243204                this.idx = (this.idx + 1) % 2;
    244 
    245                 sout | "Date thread sleeping";
    246205
    247206                sleep(1`s);
Note: See TracChangeset for help on using the changeset viewer.