* Sends the HTTP 200 OK header, the server string, for a few types of files, it * can also send the content type based on the file extension. It also sends the * content length header. Finally it send a '\r\n' in a line by itself * signalling the end of headers and the beginning of any content. * */
| 119 | * signalling the end of headers and the beginning of any content. |
| 120 | * */ |
| 121 | size_t |
| 122 | prep_headers(std::string_view path, off_t len, std::span<char> send_buffer) { |
| 123 | char format_buffer[64]; |
| 124 | const auto start = send_buffer.begin(); |
| 125 | |
| 126 | memwrite(send_buffer, "HTTP/1.0 200 OK\r\n"); |
| 127 | memwrite(send_buffer, SERVER_STRING); |
| 128 | |
| 129 | /* |
| 130 | * Check the file extension for certain common types of files |
| 131 | * on web pages and send the appropriate content-type header. |
| 132 | * Since extensions can be mixed case like JPG, jpg or Jpg, |
| 133 | * we turn the extension into lower case before checking. |
| 134 | * */ |
| 135 | std::string_view file_ext = get_filename_ext(path); |
| 136 | if ("htm"sv == file_ext || "html"sv == file_ext) { |
| 137 | memwrite(send_buffer, "Content-Type: text/html\r\n"); |
| 138 | } else if ("png"sv == file_ext) { |
| 139 | memwrite(send_buffer, "Content-Type: image/png\r\n"); |
| 140 | } else if ("gif"sv == file_ext) { |
| 141 | memwrite(send_buffer, "Content-Type: image/gif\r\n"); |
| 142 | } else if ("jpg"sv == file_ext || "jpeg"sv == file_ext) { |
| 143 | memwrite(send_buffer, "Content-Type: image/jpeg\r\n"); |
| 144 | } else if ("js"sv == file_ext) { |
| 145 | memwrite(send_buffer, "Content-Type: application/javascript\r\n"); |
| 146 | } else if ("css"sv == file_ext) { |
| 147 | memwrite(send_buffer, "Content-Type: text/css\r\n"); |
| 148 | } else if ("txt"sv == file_ext) { |
| 149 | memwrite(send_buffer, "Content-Type: text/plain\r\n"); |
| 150 | } |
| 151 | |
| 152 | (void)sprintf(format_buffer, "content-length: %ld\r\n", len); |
| 153 | // BUG check |
| 154 | memwrite(send_buffer, format_buffer); |
| 155 | memwrite(send_buffer, "\r\n"); |
| 156 | |
| 157 | return send_buffer.begin() - start; |
| 158 | } |
| 159 | |
| 160 | std::span<char> get_line(std::span<char> src) { |
| 161 | auto pos = std::string_view{src.begin(), src.end()}.find("\r\n"); |
no test coverage detected