Ignore:
Timestamp:
Jul 13, 2020, 8:07:55 PM (4 years ago)
Author:
coas-nagasima
Message:

ntshellアプリはnewlibを使うよう変更し、syscallの実装部分と区別がつくよう更新。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/ntshell/webserver/httpd.c

    r441 r442  
    5353#include "base64.h"
    5454#include "sha1.h"
    55 #include "util/ntstdio.h"
    5655#include "core/ntlibc.h"
    5756
     
    5958#define FALSE 0
    6059
    61 extern ntstdio_t *ntstdio;
    6260SYSTIM httpd_time;
    6361struct httpd_state *uploding;
     
    111109void strcpy_s(char *dst, int size, const char *src)
    112110{
    113         int slen = ntlibc_strlen(src);
     111        int slen = strlen(src);
    114112        if (slen >= size)
    115113                slen = size - 1;
     
    120118void strcat_s(char *dst, int size, const char *src)
    121119{
    122         int dlen = ntlibc_strlen(dst);
    123         int slen = ntlibc_strlen(src);
     120        int dlen = strlen(dst);
     121        int slen = strlen(src);
    124122        if (dlen + slen >= size)
    125123                slen = size - 1 - dlen;
     
    235233        struct message *m = &s->message;
    236234
    237         if (ntlibc_strncmp("Referer", buf, len) == 0) {
     235        if (strncmp("Referer", buf, len) == 0) {
    238236                m->num_headers = 1;
    239237        }
    240         else if (ntlibc_strncmp("Host", buf, len) == 0) {
     238        else if (strncmp("Host", buf, len) == 0) {
    241239                m->num_headers = 2;
    242240        }
    243         else if (ntlibc_strncmp("Upgrade", buf, len) == 0) {
     241        else if (strncmp("Upgrade", buf, len) == 0) {
    244242                m->num_headers = 3;
    245243        }
    246         else if (ntlibc_strncmp("Connection", buf, len) == 0) {
     244        else if (strncmp("Connection", buf, len) == 0) {
    247245                m->num_headers = 4;
    248246        }
    249         else if (ntlibc_strncmp("Sec-WebSocket-Key", buf, len) == 0) {
     247        else if (strncmp("Sec-WebSocket-Key", buf, len) == 0) {
    250248                m->num_headers = 5;
    251249        }
    252         else if (ntlibc_strncmp("Origin", buf, len) == 0) {
     250        else if (strncmp("Origin", buf, len) == 0) {
    253251                m->num_headers = 6;
    254252        }
    255         else if (ntlibc_strncmp("Sec-WebSocket-Protocol", buf, len) == 0) {
     253        else if (strncmp("Sec-WebSocket-Protocol", buf, len) == 0) {
    256254                m->num_headers = 7;
    257255        }
    258         else if (ntlibc_strncmp("Sec-WebSocket-Version", buf, len) == 0) {
     256        else if (strncmp("Sec-WebSocket-Version", buf, len) == 0) {
    259257                m->num_headers = 8;
    260258        }
     
    334332                        // アップロード先はSDカード
    335333                        s->filename[0] = '1';
    336                         ntstdio_printf(ntstdio, "create:    %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     334                        printf("create:    %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    337335                        if (!httpd_fs_create(s->filename, &s->file)) {
    338336                                goto error;
     
    341339                        s->in.state = IN_STATE_UPLOAD;
    342340                }
    343                 else if (ntlibc_strcmp(s->filename, uploding->filename) == 0) {
    344                         ntstdio_printf(ntstdio, "collision: %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     341                else if (strcmp(s->filename, uploding->filename) == 0) {
     342                        printf("collision: %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    345343                        goto error;
    346344                }
     
    374372
    375373        if (s->message.body_is_final) {
    376                 ntstdio_printf(ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
     374                printf("\n\n *** Error http_body_is_final() should return 1 \n"
    377375                        "on last on_body callback call "
    378376                        "but it doesn't! ***\n\n");
     
    387385
    388386        if (s->message.body_is_final) {
    389                 ntstdio_printf(ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     387                printf("close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    390388                httpd_fs_close(&s->file);
    391389                memset(&s->file, 0, sizeof(s->file));
     
    412410        struct httpd_state *s = get_context(p);
    413411        if (s->message.should_keep_alive != http_should_keep_alive(p)) {
    414                 ntstdio_printf(ntstdio, "\n\n *** Error http_should_keep_alive() should have same \n"
     412                printf("\n\n *** Error http_should_keep_alive() should have same \n"
    415413                        "value in both on_message_complete and on_headers_complete "
    416414                        "but it doesn't! ***\n\n");
     
    421419                http_body_is_final(p) &&
    422420                !s->message.body_is_final) {
    423                 ntstdio_printf(ntstdio, "\n\n *** Error http_body_is_final() should return 1 \n"
     421                printf("\n\n *** Error http_body_is_final() should return 1 \n"
    424422                        "on last on_body callback call "
    425423                        "but it doesn't! ***\n\n");
     
    493491                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    494492                if (slen < 0) {
    495                         ntstdio_printf(ntstdio, "send_file#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     493                        printf("send_file#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    496494                        s->state = STATE_CLOSING;
    497495                        break;
     
    506504                len = httpd_fs_read(&s->file, buf, len);
    507505                if (len <= 0) {
    508                         ntstdio_printf(ntstdio, "send_file#httpd_fs_read(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     506                        printf("send_file#httpd_fs_read(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    509507                        break;
    510508                }
     
    514512
    515513                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    516                         ntstdio_printf(ntstdio, "send_file#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     514                        printf("send_file#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    517515                        s->state = STATE_CLOSING;
    518516                        break;
     
    520518        }
    521519
    522         ntstdio_printf(ntstdio, "close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     520        printf("close:     %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    523521        httpd_fs_close(&s->file);
    524522        s->file.len = 0;
     
    536534                slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    537535                if (slen < 0) {
    538                         ntstdio_printf(ntstdio, "send_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     536                        printf("send_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    539537                        s->state = STATE_CLOSING;
    540538                        break;
     
    553551
    554552                if ((slen = tcp_snd_buf(s->cepid, len)) != E_OK) {
    555                         ntstdio_printf(ntstdio, "send_data#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     553                        printf("send_data#tcp_snd_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    556554                        s->state = STATE_CLOSING;
    557555                        break;
     
    571569        char *ptr;
    572570
    573         len = ntlibc_strlen(statushdr);
     571        len = strlen(statushdr);
    574572        tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
    575573
     
    586584                        tcp_snd_dat(s->cepid, "/~", len, TMO_FEVR);
    587585                }
    588                 len = ntlibc_strlen(s->filename);
     586                len = strlen(s->filename);
    589587                tcp_snd_dat(s->cepid, s->filename, len, TMO_FEVR);
    590588                if (s->query != NULL) {
    591589                        tcp_snd_dat(s->cepid, "?", 1, TMO_FEVR);
    592                         len = ntlibc_strlen(s->query);
     590                        len = strlen(s->query);
    593591                        tcp_snd_dat(s->cepid, s->query, len, TMO_FEVR);
    594592                }
     
    602600                tcp_snd_dat(s->cepid, (void *)http_content_type_binary, len, TMO_FEVR);
    603601        }
    604         else if (ntlibc_strncmp(http_html, ptr, sizeof(http_html) - 1) == 0 ||
    605                 ntlibc_strncmp(http_htm, ptr, sizeof(http_htm) - 1) == 0) {
     602        else if (strncmp(http_html, ptr, sizeof(http_html) - 1) == 0 ||
     603                strncmp(http_htm, ptr, sizeof(http_htm) - 1) == 0) {
    606604                len = sizeof(http_content_type_html) - 1;
    607605                tcp_snd_dat(s->cepid, (void *)http_content_type_html, len, TMO_FEVR);
    608606        }
    609         else if (ntlibc_strncmp(http_css, ptr, sizeof(http_css) - 1) == 0) {
     607        else if (strncmp(http_css, ptr, sizeof(http_css) - 1) == 0) {
    610608                len = sizeof(http_content_type_css) - 1;
    611609                tcp_snd_dat(s->cepid, (void *)http_content_type_css, len, TMO_FEVR);
    612610        }
    613         else if (ntlibc_strncmp(http_js, ptr, sizeof(http_js) - 1) == 0) {
     611        else if (strncmp(http_js, ptr, sizeof(http_js) - 1) == 0) {
    614612                len = sizeof(http_content_type_js) - 1;
    615613                tcp_snd_dat(s->cepid, (void *)http_content_type_js, len, TMO_FEVR);
    616614        }
    617         else if (ntlibc_strncmp(http_json, ptr, sizeof(http_json) - 1) == 0) {
     615        else if (strncmp(http_json, ptr, sizeof(http_json) - 1) == 0) {
    618616                len = sizeof(http_content_type_json) - 1;
    619617                tcp_snd_dat(s->cepid, (void *)http_content_type_json, len, TMO_FEVR);
    620618        }
    621         else if (ntlibc_strncmp(http_png, ptr, sizeof(http_png) - 1) == 0) {
     619        else if (strncmp(http_png, ptr, sizeof(http_png) - 1) == 0) {
    622620                len = sizeof(http_content_type_png) - 1;
    623621                tcp_snd_dat(s->cepid, (void *)http_content_type_png, len, TMO_FEVR);
    624622        }
    625         else if (ntlibc_strncmp(http_gif, ptr, sizeof(http_gif) - 1) == 0) {
     623        else if (strncmp(http_gif, ptr, sizeof(http_gif) - 1) == 0) {
    626624                len = sizeof(http_content_type_gif) - 1;
    627625                tcp_snd_dat(s->cepid, (void *)http_content_type_gif, len, TMO_FEVR);
    628626        }
    629         else if (ntlibc_strncmp(http_jpg, ptr, sizeof(http_jpg) - 1) == 0) {
     627        else if (strncmp(http_jpg, ptr, sizeof(http_jpg) - 1) == 0) {
    630628                len = sizeof(http_content_type_jpg) - 1;
    631629                tcp_snd_dat(s->cepid, (void *)http_content_type_jpg, len, TMO_FEVR);
    632630        }
    633         else if (ntlibc_strncmp(http_svg, ptr, sizeof(http_svg) - 1) == 0) {
     631        else if (strncmp(http_svg, ptr, sizeof(http_svg) - 1) == 0) {
    634632                len = sizeof(http_content_type_svg) - 1;
    635633                tcp_snd_dat(s->cepid, (void *)http_content_type_svg, len, TMO_FEVR);
    636634        }
    637         else if (ntlibc_strncmp(http_text, ptr, sizeof(http_text) - 1) == 0) {
     635        else if (strncmp(http_text, ptr, sizeof(http_text) - 1) == 0) {
    638636                len = sizeof(http_content_type_text) - 1;
    639637                tcp_snd_dat(s->cepid, (void *)http_content_type_text, len, TMO_FEVR);
    640638        }
    641         else if (ntlibc_strncmp(http_eot, ptr, sizeof(http_eot) - 1) == 0) {
     639        else if (strncmp(http_eot, ptr, sizeof(http_eot) - 1) == 0) {
    642640                len = sizeof(http_content_type_eot) - 1;
    643641                tcp_snd_dat(s->cepid, (void *)http_content_type_eot, len, TMO_FEVR);
    644642        }
    645         else if (ntlibc_strncmp(http_ttf, ptr, sizeof(http_ttf) - 1) == 0) {
     643        else if (strncmp(http_ttf, ptr, sizeof(http_ttf) - 1) == 0) {
    646644                len = sizeof(http_content_type_ttf) - 1;
    647645                tcp_snd_dat(s->cepid, (void *)http_content_type_ttf, len, TMO_FEVR);
    648646        }
    649         else if (ntlibc_strncmp(http_woff, ptr, sizeof(http_woff) - 1) == 0) {
     647        else if (strncmp(http_woff, ptr, sizeof(http_woff) - 1) == 0) {
    650648                len = sizeof(http_content_type_woff) - 1;
    651649                tcp_snd_dat(s->cepid, (void *)http_content_type_woff, len, TMO_FEVR);
    652650        }
    653         else if (ntlibc_strncmp(http_woff2, ptr, sizeof(http_woff2) - 1) == 0) {
     651        else if (strncmp(http_woff2, ptr, sizeof(http_woff2) - 1) == 0) {
    654652                len = sizeof(http_content_type_woff2) - 1;
    655653                tcp_snd_dat(s->cepid, (void *)http_content_type_woff2, len, TMO_FEVR);
    656654        }
    657         else if (ntlibc_strncmp(http_ico, ptr, sizeof(http_ico) - 1) == 0) {
     655        else if (strncmp(http_ico, ptr, sizeof(http_ico) - 1) == 0) {
    658656                len = sizeof(http_content_type_ico) - 1;
    659657                tcp_snd_dat(s->cepid, (void *)http_content_type_ico, len, TMO_FEVR);
     
    667665                len = sizeof(http_content_length) - 1;
    668666                tcp_snd_dat(s->cepid, (void *)http_content_length, len, TMO_FEVR);
    669                 len = ntstdio_snprintf(s->temp, sizeof(s->temp), "%d\r\n", s->file.len);
     667                len = snprintf(s->temp, sizeof(s->temp), "%d\r\n", s->file.len);
    670668                tcp_snd_dat(s->cepid, (void *)s->temp, len, TMO_FEVR);
    671669        }
     
    699697                break;
    700698        case OUT_STATE_OPEN_GET_FILE:
    701                 ntstdio_printf(ntstdio, "open:      %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
     699                printf("open:      %s.%d %s\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->filename);
    702700                if (!httpd_fs_open(s->filename, sizeof(s->message.request_url), &s->file)) {
    703701                        s->filename = NULL;
     
    713711                break;
    714712        case OUT_STATE_WAIT_POST_BODY:
    715                 //ntstdio_printf(ntstdio, "wait post body\n");
     713                //printf("wait post body\n");
    716714                s->out.wait = true;
    717715                break;
    718716        case OUT_STATE_BODY_RECEIVED:
    719                 //ntstdio_printf(ntstdio, "body received\n");
     717                //printf("body received\n");
    720718                s->out.statushdr = http_header_200;
    721719                s->out.state = OUT_STATE_SEND_HEADER;
    722720                break;
    723721        case OUT_STATE_SEND_HEADER:
    724                 //ntstdio_printf(ntstdio, "send header\n");
     722                //printf("send header\n");
    725723                send_headers(s, s->out.statushdr);
    726724                break;
    727725        case OUT_STATE_SEND_FILE:
    728                 //ntstdio_printf(ntstdio, "send file %d\n", s->file.len);
     726                //printf("send file %d\n", s->file.len);
    729727                send_file(s);
    730728                break;
    731729        case OUT_STATE_SEND_DATA:
    732                 //ntstdio_printf(ntstdio, "send data %d\n", s->response_len);
     730                //printf("send data %d\n", s->response_len);
    733731                send_data(s);
    734732                break;
    735733        case OUT_STATE_SEND_END:
    736                 //ntstdio_printf(ntstdio, "send end\n");
     734                //printf("send end\n");
    737735                s->out.wait = true;
    738736                if (s->message.should_keep_alive && s->reset == 0) {
     
    750748        int len;
    751749
    752         len = ntlibc_strlen(statushdr);
     750        len = strlen(statushdr);
    753751        tcp_snd_dat(s->cepid, (void *)statushdr, len, TMO_FEVR);
    754752
    755753        len = sizeof(http_upgrade) - 1;
    756754        tcp_snd_dat(s->cepid, (void *)http_upgrade, len, TMO_FEVR);
    757         len = ntlibc_strlen(s->message.upgrade);
     755        len = strlen(s->message.upgrade);
    758756        tcp_snd_dat(s->cepid, s->message.upgrade, len, TMO_FEVR);
    759757        len = sizeof(http_crnl) - 1;
     
    762760        len = sizeof(http_connection) - 1;
    763761        tcp_snd_dat(s->cepid, (void *)http_connection, len, TMO_FEVR);
    764         len = ntlibc_strlen(s->message.connection);
     762        len = strlen(s->message.connection);
    765763        tcp_snd_dat(s->cepid, s->message.connection, len, TMO_FEVR);
    766764        len = sizeof(http_crnl) - 1;
     
    769767        len = sizeof(http_sec_websocket_accept) - 1;
    770768        tcp_snd_dat(s->cepid, (void *)http_sec_websocket_accept, len, TMO_FEVR);
    771         len = ntlibc_strlen(s->message.response_key);
     769        len = strlen(s->message.response_key);
    772770        tcp_snd_dat(s->cepid, s->message.response_key, len, TMO_FEVR);
    773771        len = sizeof(http_crnl) - 1;
     
    776774        len = sizeof(http_sec_websocket_protocol) - 1;
    777775        tcp_snd_dat(s->cepid, (void *)http_sec_websocket_protocol, len, TMO_FEVR);
    778         len = ntlibc_strlen(s->message.sec_websocket_protocol);
     776        len = strlen(s->message.sec_websocket_protocol);
    779777        tcp_snd_dat(s->cepid, s->message.sec_websocket_protocol, len, TMO_FEVR);
    780778        len = sizeof(http_crnl) - 1;
     
    792790        slen = tcp_get_buf(s->cepid, (void **)&buf, TMO_FEVR);
    793791        if (slen < 0) {
    794                 ntstdio_printf(ntstdio, "send_ws_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
     792                printf("send_ws_data#tcp_get_buf(%s.%d) => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, slen);
    795793                return;
    796794        }
     
    856854                                //}
    857855                        }
    858                         ntstdio_printf(ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     856                        printf("handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    859857                        uploding = NULL;
    860858                        s->state = STATE_CLOSING;
     
    864862                tcp_rel_buf(s->cepid, done);
    865863                if (s->parser.http_errno != HPE_OK) {
    866                         ntstdio_printf(ntstdio, "http_parser error %s.%d => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
     864                        printf("http_parser error %s.%d => %d\n", s->addr, ((T_IPV4EP *)s->dst)->portno, s->parser.http_errno);
    867865                        uploding = NULL;
    868866                        s->state = STATE_CLOSING;
     
    889887                                        break;
    890888                                }
    891                                 ntstdio_printf(ntstdio, "handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
     889                                printf("handle_input#tcp_rcv_buf#%d(%s.%d) => %d\n", s->in.state, s->addr, ((T_IPV4EP *)s->dst)->portno, len);
    892890                                s->state = STATE_CLOSING;
    893891                                break;
     
    927925
    928926        if (s == NULL)
    929                 ntstdio_printf(ntstdio, "callback_nblk_tcp(%d, %d)\n", fncd, cepid);
     927                printf("callback_nblk_tcp(%d, %d)\n", fncd, cepid);
    930928        else
    931                 ntstdio_printf(ntstdio, "callback_nblk_tcp(%d, %s.%d)\n", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
     929                printf("callback_nblk_tcp(%d, %s.%d)\n", fncd, s->addr, ((T_IPV4EP *)s->dst)->portno);
    932930
    933931        return E_PAR;
     
    945943                ret2 = get_tim(&httpd_time);
    946944                if (ret2 != E_OK) {
    947                         ntstdio_printf(ntstdio, "get_tim\n");
     945                        printf("get_tim\n");
    948946                        return;
    949947                }
     
    953951                        memset(&s->dst, 0, sizeof(s->dst));
    954952                        if ((ret = tcp_acp_cep(s->cepid, TCP_REPID, (T_IPV4EP *)s->dst, TMO_FEVR)) != E_OK) {
    955                                 ntstdio_printf(ntstdio, "tcp_acp_cep(%d) => %d\n", s->cepid, ret);
     953                                printf("tcp_acp_cep(%d) => %d\n", s->cepid, ret);
    956954                                tslp_tsk(100 * 1000);   // TODO
    957955                                s->state = STATE_CLOSING;
     
    959957                        }
    960958                        IP2STR(s->addr, &((T_IPV4EP *)s->dst)->ipaddr);
    961                         ntstdio_printf(ntstdio, "connected: %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     959                        printf("connected: %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
    962960                        memset(&s->in, 0, sizeof(s->in));
    963961                        memset(&s->out, 0, sizeof(s->out));
     
    974972                        break;
    975973                case STATE_CLOSING:
    976                         ntstdio_printf(ntstdio, "close:     %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
     974                        printf("close:     %s.%d\n", s->addr, ((T_IPV4EP *)s->dst)->portno);
    977975                        tcp_sht_cep(s->cepid);
    978976                        tcp_cls_cep(s->cepid, TMO_FEVR);
Note: See TracChangeset for help on using the changeset viewer.