Skip to content

Conversation

@maeken4
Copy link
Owner

@maeken4 maeken4 commented Nov 15, 2025

最低限要件を満たすhttpサーバーを実装しました。
後回しにしているエラー処理はコメントしているものもありますが、考慮できていない点がありましたらご指摘頂けると幸いです。

Copy link

@akmhmgc akmhmgc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とりあえずclient側だけ読んでコメントしました!読みやすかったです。
残りも後で確認します!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

サーバー側の名前がhttp_server.cなのでhttp_client.cだとわかりやすいと思いました!

"Connection: close\r\n"
"\r\n",
SERVER_ADDR, SERVER_PORT);
send(client_fd, request, request_len, 0); // TODO: 0以外のflagの挙動を調べる
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

サーバー側のコネクションがcloseしているとここでプロセスが落ちるのでMSG_NOSIGNALをつけてベットエラーハンドリングをした方が良いかもしれないです。

MSG_NOSIGNAL (Linux 2.2 以降)
ストリーム指向のソケットで相手側が接続を切断した時に、エラーとして SIGPIPE を送信しないように要求する。
https://ja.manpages.org/send/2

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendは例えばカーネルの送信バッファーに十分な空きがない時にはデータが一部しか送信されない可能性があるようです。
なのでデータを送り切るまでループすると良いと思います。

https://stackoverflow.com/questions/8900474/when-will-send-return-less-than-the-length-argument

"GET /calc?query=12+24 HTTP/1.1\r\n"
"Host: %s:%s\r\n"
"User-Agent: simple-c-http-client/1.0\r\n"
"Connection: close\r\n"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeepAlive ではないのでConnection: closeをつけているのが丁寧で良いと思いました!

Comment on lines +63 to +64
char buf[BUF_SIZE];
int response_len = recv(client_fd, buf, BUF_SIZE, 0); // BUF_SIZEより大きいと動かない
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mallocで動的にメモリを確保してレスポンスを受け取る書き方も試しても良いかもしれません。

Copy link

@akmhmgc akmhmgc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一通りコメントしました!

int parse_and_calc(char* calc_query, int* res) {
// calc_queryはquery=2+3の形式
char* p = strchr(calc_query, '=');
if (strncmp(calc_query, "query", p - calc_query) != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strncmpの第三引数の比較する文字列の長さをp - calc_queryで決めているので例えばque=2+3の場合でも0を返すことになるので、queryという文字列の長さを指定するのが良さそうです。

free(req);
}

void hundle_http_req(FILE* in, FILE* out) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleのtypoでしょうか?

struct HTTPHeaderField {
char* field;
char* value;
struct HTTPHeaderField* next;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedListのような感じでヘッダーを最後の行まで見る方法は思いつかなかったので勉強になりました。

query_param = strchr(http_req->uri, '?');

// pathが正しいか
if (strncmp(http_req->uri, "/calc", query_param - http_req->uri) != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +121 to +123
free(req->method);
free(req->uri);
free(req->body);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req->http_verのfreeが漏れてると思います。

hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_PASSIVE; // bind用
// result(ポインタ)へのポインタを渡す(getaddrinfoの第4引数はポインタへのポインタ)
err = getaddrinfo(NULL, "8090", &hints, &result);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
clientに合わせてポート番号は定数化しても良いかもしれません。

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_PASSIVE; // bind用
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分はhistsを使わずに直接INADDR_ANYを指定していたので参考になりました!

}

// fdopen: ファイルディスクリプタを扱いやすいFILE*でラップする
FILE* input_file_stream = fdopen(sock_fd, "r");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FILEでラップする方法は知りませんでした。参考になります。

Comment on lines +82 to +83
fflush(output_file_stream);
close(sock_fd);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fclose() 関数は、 stream が指すストリームを (バッファーリングされて いた全ての出力データを fflush(3) を用いて書き込んで) フラッシュし、 対応するファイルディスクリプターをクローズする
https://linuxjm.sourceforge.io/html/LDP_man-pages/man3/fclose.3.html

とあるので、ここでinput_file_streamoutput_file_stream両方をfcloseで閉じると良いと思いました。

Comment on lines +68 to +70
if (sock_fd < 0) {
exit(EXIT_FAILURE);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EINTRなどのエラーに関してはリトライしても良いかもしれません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants