+/* Read HTTP request line and headers (ignored).
+ *
+ * On success 0 is returned, -1 on client error (we close client descriptor in
+ * this case), -2 on unexpected EOF.
+ */
+static int read_http_request(FILE *client_fd, char *request, size_t length) {
+ char buffer[MAX_REQUEST_LINE];
+
+ if (NULL == fgets(request, (int)length, client_fd)) {
+ if (ferror(client_fd)) {
+ perror("fgets(), request");
+ fclose(client_fd);
+ return -1;
+ }
+
+ return -2;
+ }
+
+ while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
+ /* End of header. */
+ if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
+ break;
+ }
+ }
+ if (ferror(client_fd)) {
+ perror("fgets(), header");
+ fclose(client_fd);
+ return -1;
+ }
+
+ return 0;
+}
+