TT Lab
Get started
Learn Learning paths Courses

Linux Network Diagnosis

HTTP Diagnosis

Continue in TT Lab

Goal

You read an HTTP response along four axes, status code, headers, timing, and body, and tie it together into a health check script.

Why it matters

What is needed when you receive a report that "responses are slow" is not more dashboards but splitting the time of a single request by segment. The timing variables of curl -w tell you name resolution / TCP connection / TLS / server first byte / body transfer separately. If everything is normal up to time_connect and only time_starttransfer is large, the network is innocent and the application is the culprit - the debate ends right there. And a health check must always have a maximum wait time. Without it, the script stops forever when the other side does not respond, and if cron launches it every 5 minutes, processes pile up.

Steps

Use /root/www as the document root and /root/http as the working directory.

  1. Create /root/www/index.html containing labhub-http-ok, start python3 -m http.server 8000 --bind 127.0.0.1 --directory /root/www in the background, and write its PID to /root/http/server_pid.txt.
  2. Write only the number of the status code for a request to a nonexistent path to /root/http/status404.txt.
  3. Receive only the response headers of /index.html and save them to /root/http/headers.txt. The body must not be mixed in.
  4. Save the three values time_namelookup, time_connect, and time_total of the request on one line separated by spaces to /root/http/timing.txt.
  5. Create /root/www/api.json. name must be labhub, and tags must be an array with two or more values. Then fetch that file from the server and write the second value of tags to /root/http/tag.txt.
  6. Create the directory /root/www/sub and put one file in it. Write the status codes of requesting http://127.0.0.1:8000/sub without following redirects and with following them to /root/http/redirect.txt as the following two lines.
    • no_follow=<코드> (the code)
    • follow=<코드> (the code)
  7. Write the status code for sending a POST request to /root/http/method.txt in the form post=<코드> (the code goes where the Korean word is).
  8. Create /root/http/healthcheck.sh <URL>.
    • If 2xx, print ok code=<코드> time=<초> (the code and the seconds) and exit with code 0
    • If a response came but it is not 2xx, print down code=<코드> (the code) and exit with code 1
    • If the connection itself fails, print a message starting with down and exit with code 1. You must decide within 10 seconds.

Notes

Start a local HTTP server

Create /root/www/index.html containing labhub-http-ok, start python3 -m http.server 8000 --bind 127.0.0.1 --directory /root/www in the background, and write its PID to /root/http/server_pid.txt.

python3 -m http.server has an option to specify the document root. Bind it only to 127.0.0.1.

Extract only the status code

Write only the number of the status code for a request to a nonexistent path to /root/http/status404.txt.

There is a -w variable that discards the body and prints only the status code.

Receive only headers

Receive only the response headers of /index.html and save them to /root/http/headers.txt. The body must not be mixed in.

It is the option that sends a HEAD request. The body must not be mixed into the file.

Measure the time per segment

Save the three values time_namelookup, time_connect, and time_total of the request on one line separated by spaces to /root/http/timing.txt.

Print the three of -w's time_namelookup, time_connect, and time_total on one line separated by spaces.

Extract a value from a JSON response

Create /root/www/api.json. name must be labhub, and tags must be an array with two or more values. Then fetch that file from the server and write the second value of tags to /root/http/tag.txt.

Pass curl's output to jq. Array indexes start at 0.

Follow a redirect

Create the directory /root/www/sub and put one file in it. Write the status codes of requesting http://127.0.0.1:8000/sub without following redirects and with following them to /root/http/redirect.txt as the following two lines.

By default curl does not follow redirects. If a directory path has no trailing slash, the server returns a 301.

Response for an unimplemented method

Write the status code for sending a POST request to /root/http/method.txt in the form post=<코드> (the code goes where the Korean word is).

Specify the method with -X. This server implements only GET and HEAD.

Health check script

Create /root/http/healthcheck.sh <URL>.

You must distinguish three cases: healthy, non-2xx, and unreachable. Always set a maximum wait time.