POST

HTTPメソッドのPOSTを使って、サーバにデータを送信する。

POST - HTTP | MDN

とはいえ、サーバは送られたリクエストをどう処理するか、Apacheでデフォルトの処理みたいなのは決められていない(それらの処理はCGIなどを作って自分で処理する。)ので、今回はログだけ出してみる。
前回のGETのリクエストの場合、

$ sudo cat /var/log/httpd/access_log

アクセスログを見ると、最後の方で、

xx.xxx.xx.xxx - - [xx/xxx/2021:xx:xx:xx +0900] "GET /index.html HTTP/1.0" 200 484 "-" "-"
xxx.xxx.xx.xx - - [xx/xxx/2021:xx:xx:xx +0900] "GET /cgi-bin/test.cgi HTTP/1.1" 200 88 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
$

こう出てる。
上は前回の自作クライアントでindex.html、下はChromecgi-binの下のtest.cgiにアクセス。
では、POSTにしてみる。

このように書く。
argv[4]がPOSTで送る文字列。
一応、Content-Lengthや、Content-Typeを付けて、"ABCDE"という単純な文字列をHTTPボディに。

$ vi http_client.c
$ gcc http_client.c -o http_client
$ ./http_client bgstation0.com 80 /cgi-bin/test.cgi ABCDE
connect success.
HTTP/1.1 200 OK
Date: xxx, xx xxx 2021 xx:xx:xx GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips
Connection: close
Content-Type: text/html

<html>
<head>
<title>hello</title>
</head>
<body>
<p>
Howdy, Perl!
</p>
</body>
</html>

$

"/cgi-bin/test.cgi"にアクセスしてるけど、これ自体はあまり意味ない。
アクセスログを見ると、

xx.xx.xx.xxx - - [xx/xxx/xxxx:xx:xx:xx +0900] "POST /cgi-bin/test.cgi HTTP/1.0" 200 88 "-" "-"
$

POSTでもログは残ってるなあ。

HTTP POST でデータを送って中身を確認したい - Qiita

ログに記録されないのはPOSTで送ったBODYのことかな。
ちゃんとログに記録されるかconfを調べる。

$ sudo vi /etc/httpd/conf/httpd.conf

これで"LoadModule"で検索すると、

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf

conf.modules.dの下の.confを見ればいいのか。

$ sudo vi /etc/httpd/conf.modules.d/00-base.conf

ここを見てたら、

LoadModule dumpio_module modules/mod_dumpio.so

mod_dumpio.soは既に入ってた。
ただ、error_logにはそれっぽいログが無かったので、

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf
DumpIOInput On

"DumpIOInput On"を追加。

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
#LogLevel warn
LogLevel debug dumpio:trace7

ログレベルも変更。

$ sudo systemctl restart httpd.service

再起動。
で、またPOSTで投げてみる。

$ sudo cat /var/log/httpd/error_log

エラーログを見ると、

[xxx xxx xx xx:xx:xx.561261 xxxx] [dumpio:trace7] [pid 20778] mod_dumpio.c(140): [client xx.xxx.xx.xxx:56296] mod_dumpio: dumpio_in [readbytes-blocking] 5 readbytes
[xxx xxx xx xx:xx:xx.561307 2021] [dumpio:trace7] [pid 20778] mod_dumpio.c(63): [client xx.xxx.xx.xxx:56296] mod_dumpio:  dumpio_in (data-HEAP): 5 bytes
[xxx xxx xx xx:xx:xx.561317 2021] [dumpio:trace7] [pid 20778] mod_dumpio.c(103): [client xx.xxx.xx.xxx:56296] mod_dumpio:  dumpio_in (data-HEAP): ABCDE
$

こんな感じでPOSTのBODYにある"ABCDE"が見れた。
読み込んだバイト数も出てる。
というか、この前にヘッダのContent-Lengthとか、Content-Typeとかも、値がいくつかとか出てる。

Sample/http/http_method/POST/src/http_method at master · bg1bgst333/Sample · GitHub