Conversation
YUTA-Uchi
left a comment
There was a problem hiding this comment.
一旦急ぎで見つけた部分をレビューしました。途中から自分が作った方にもたくさん不備がみつかってきたので私の方もプルリク上げたいと思います
| break; | ||
| } | ||
|
|
||
| // "\r\n\r\n" (4文字) が読み込み境界を跨ぐ可能性を考慮し、3バイト戻ったところから探索する |
There was a problem hiding this comment.
ここから64行目までの処理は特定のOSのための処理でしょうか?
\r\nが出てくるとWindows向けの特別な処理をやっているように見えました。(\r\nがWindowsの改行文字なので)もし特別処理であればその処理だけ関数別にしたほうがわかりやすい気がします。
There was a problem hiding this comment.
(正直書いた時は詳しく把握できていませんでしたが、)これは送られてきているリクエストからヘッダーを切り離す(空行を見つける)処理で、RFC9112で改行は\r\n(CRLF)で表すように決められているみたいなので、特にOS固有の処理ではなく、ヘッダーがここまでだというのを判断するためにOS共通で使用しているロジック、という感じです。
プログラムを書いた時には全然見られていませんでしたが、本当はこちらに書いてある"The normal procedure"に従ってリクエストを読み込むのが良さそうかもしれません。
| } | ||
|
|
||
| if (strncmp(read_buffer, path_prefix, strlen(path_prefix)) != 0) { | ||
| respond_and_close(connection_fd, 404, "The requested URL was not found on this server\n"); |
There was a problem hiding this comment.
しっかり追えているか不安ですが、ここでfree(read_buffer);したほうがメモリリークがないかもです
There was a problem hiding this comment.
あ、これは必要そうですね!メモリリークを起こしていそうです。L97にも必要だと思います。
| } | ||
|
|
||
| if (strncmp(read_buffer + strlen(path_prefix), query_prefix, strlen(query_prefix)) != 0) { | ||
| respond_and_close(connection_fd, 400, "Failed to find query"); |
There was a problem hiding this comment.
ここも同様です。free(read_buffer);したほうがメモリリークがないかもです
| @@ -0,0 +1,8 @@ | |||
| コンパイル・実行(Unix/Linux環境): | |||
There was a problem hiding this comment.
https://github.com/potrue/leetcode/pull/81/changes#r2781804952
もし上記の処理がWindows向けであるならば、ここも変更が必要か検討が必要かと思います
| }; | ||
|
|
||
| if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { | ||
| perror("bind"); |
| } | ||
|
|
||
| if (listen(socket_fd, BACKLOG_SIZE) < 0) { | ||
| perror("listen"); |
| return; | ||
| } | ||
|
|
||
| write(connection_fd, response, response_len); |
|
|
||
| static char* read_request(int connection_fd) { | ||
| size_t capacity = INITIAL_READ_BUFFER_SIZE; | ||
| char* read_buffer = malloc(capacity); |
| return; | ||
| } | ||
|
|
||
| if (strncmp(read_buffer, path_prefix, strlen(path_prefix)) != 0) { |
There was a problem hiding this comment.
/calc で始まれば、他の path でも区別して動いてしまう
GET /calcium?hoge=...
| return; | ||
| } | ||
|
|
||
| if (strncmp(read_buffer + strlen(path_prefix), query_prefix, strlen(query_prefix)) != 0) { |
There was a problem hiding this comment.
query 引数が第1引数だった時以外は動かない?
/calc?utm_source=github&query=1+2
|
|
||
| char* query_start = read_buffer + strlen(path_prefix) + strlen(query_prefix); | ||
| int a, b; | ||
| if (sscanf(query_start, "%d+%d", &a, &b) != 2) { |
| @@ -0,0 +1,6 @@ | |||
| #ifndef UTILS_H | |||
| @@ -0,0 +1,8 @@ | |||
| コンパイル・実行(Unix/Linux環境): | |||
|
|
|||
| `gcc -o server server.c utils.c` | |||
|
|
||
| 動作確認: | ||
|
|
||
| `curl http://127.0.0.1:8080/calc\?query\=1+11` など |
There was a problem hiding this comment.
これブラウザからだとどう入力するのが正しくなりますか? (url encoding)
|
|
||
| int connection_fd = accept(socket_fd, (struct sockaddr*)&client_addr, &client_addr_size); | ||
| if (connection_fd < 0) { | ||
| perror("accept"); |
| total_read_size += read_size; | ||
| read_buffer[total_read_size] = '\0'; | ||
|
|
||
| if (strstr(read_buffer + str_search_offset, "\r\n\r\n")) { |
There was a problem hiding this comment.
データを全部読み切らずに、改行2つで早期に止めるのはどういうケースを想定をしていますか?
(keep alive?)
No description provided.