-
Notifications
You must be signed in to change notification settings - Fork 0
implement minimum server and client #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
akmhmgc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
とりあえずclient側だけ読んでコメントしました!読みやすかったです。
残りも後で確認します!
There was a problem hiding this comment.
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の挙動を調べる |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KeepAlive ではないのでConnection: closeをつけているのが丁寧で良いと思いました!
| char buf[BUF_SIZE]; | ||
| int response_len = recv(client_fd, buf, BUF_SIZE, 0); // BUF_SIZEより大きいと動かない |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mallocで動的にメモリを確保してレスポンスを受け取る書き方も試しても良いかもしれません。
akmhmgc
left a comment
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| free(req->method); | ||
| free(req->uri); | ||
| free(req->body); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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用 |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FILEでラップする方法は知りませんでした。参考になります。
| fflush(output_file_stream); | ||
| close(sock_fd); |
There was a problem hiding this comment.
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_streamとoutput_file_stream両方をfcloseで閉じると良いと思いました。
| if (sock_fd < 0) { | ||
| exit(EXIT_FAILURE); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EINTRなどのエラーに関してはリトライしても良いかもしれません。
最低限要件を満たすhttpサーバーを実装しました。
後回しにしているエラー処理はコメントしているものもありますが、考慮できていない点がありましたらご指摘頂けると幸いです。