A student writes this CGI program to echo a POST request body back to the browser. The standard headers are included and the program compiles without warnings.
int main(void) { char buf[256]; int n = atoi(getenv("CONTENT_LENGTH")); fread(buf, 1, n, stdin); printf("Got: %s\n", buf); return 0; }
Which of the following statements about this program are true? Select all that apply.
If the request has no body, CONTENT_LENGTH may be unset, so getenv returns NULL and the call atoi(NULL) has undefined behaviour.
The output has no Content-Type header and blank line, so the server cannot treat what the program prints as a valid CGI response.
The program should keep calling fread until it reaches end of file, rather than trusting the byte count in CONTENT_LENGTH.
A body longer than 256 bytes overflows buf, and even a shorter body is not null-terminated when printf reads it with %s.
The server percent-decodes the body before writing it to standard input, so buf already holds the plain field values.
Unlock full course notes by signing up
Decoding a query-string value and sizing its buffer
A CGI program receives a GET request whose QUERY_STRING environment variable is set to
msg=Hi%21+5%2B3%3D8
Consider only the value of msg, the text after msg=. Find the length of the encoded value in bytes, the number of %XX escapes it contains, the length of the decoded value in bytes, and the smallest char array size in bytes that holds the decoded value as a C string. Give each as a whole number.
Lifecycle of a CGI POST request
A browser submits a form with method POST to a CGI script written in C. Put the stages of handling this single request in the order they occur, from the browser's request to the reply it receives.
Meta-variables and stdin bytes for three requests
A web server receives the three HTTP requests below. Each request is mapped to a C program in /cgi-bin/. For each request, enter the values of REQUEST_METHOD and QUERY_STRING exactly as the server sets them. Also enter the number of bytes the program should read from standard input. Bodies are shown exactly as transmitted, with no trailing newline.
Cost of starting a process for every CGI request
A small server runs a C order-lookup program through CGI on a machine with 4 CPU cores. Profiling shows two costs for each request. Creating the process and starting the program takes 12 ms of CPU time; this covers fork, exec, dynamic linking and opening a database connection. Handling the request itself takes a further 3 ms of CPU time. Assume the work is CPU-bound, requests spread evenly over the cores, and nothing else runs on the machine.
Find:
the CPU time per request, in ms, as a whole number;