void http_writer_response(struct http_writer *w, int code) { struct http_status *status = http_status_lookup(code); assert(w->state == HTTP_WRITER_INIT); if (!status) die("unknown HTTP status code %d", code); growbuf_reset(&w->prebody); growbuf_append(&w->prebody, status->message, status->message_len); w->state = HTTP_WRITER_HEADERS; }
/** * Read from the file to the next end of line. * * Args: * input - file to read from * bytes_read - set to the number of bytes read * * Returns: * Buffer containing the read result. */ char* read_to_eol(FILE* input, size_t* bytes_read) { char* retbuf = NULL; char buf[512]; growbuf* gb = NULL; gb = growbuf_create(512); while (!feof(input)) { size_t chunk_bytes_read = fread(buf, 1, sizeof(buf), input); bool got_newline = false; size_t bytes_append; for (bytes_append = 0; bytes_append < chunk_bytes_read; bytes_append++) { if (buf[bytes_append] == '\n') { got_newline = true; break; } } growbuf_append(gb, buf, bytes_append); if (got_newline) { DEBUG fprintf(stderr, "read %zu bytes, newline after %zu, rewinding %zu\n", chunk_bytes_read, bytes_append, chunk_bytes_read-bytes_append); fseek(input, -1 * (chunk_bytes_read - bytes_append - 1), SEEK_CUR); goto cleanup; } } cleanup: // // free just the growbuf structure, and return the inner buffer // retbuf = gb->buf; *bytes_read = gb->size; free(gb); return retbuf; }