mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-28 14:23:30 +01:00
Refactor write_http_response_header_impl() to use buf_t
This commit is contained in:
parent
d5ba4851bd
commit
5240d02a11
@ -3503,63 +3503,47 @@ write_http_response_header_impl(dir_connection_t *conn, ssize_t length,
|
|||||||
long cache_lifetime)
|
long cache_lifetime)
|
||||||
{
|
{
|
||||||
char date[RFC1123_TIME_LEN+1];
|
char date[RFC1123_TIME_LEN+1];
|
||||||
char tmp[1024];
|
|
||||||
char *cp;
|
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
|
buf_t *buf = buf_new_with_capacity(1024);
|
||||||
|
|
||||||
tor_assert(conn);
|
tor_assert(conn);
|
||||||
|
|
||||||
format_rfc1123_time(date, now);
|
format_rfc1123_time(date, now);
|
||||||
cp = tmp;
|
|
||||||
tor_snprintf(cp, sizeof(tmp),
|
buf_add_printf(buf, "HTTP/1.0 200 OK\r\nDate: %s\r\n", date);
|
||||||
"HTTP/1.0 200 OK\r\nDate: %s\r\n",
|
|
||||||
date);
|
|
||||||
cp += strlen(tmp);
|
|
||||||
if (type) {
|
if (type) {
|
||||||
tor_snprintf(cp, sizeof(tmp)-(cp-tmp), "Content-Type: %s\r\n", type);
|
buf_add_printf(buf, "Content-Type: %s\r\n", type);
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (!is_local_addr(&conn->base_.addr)) {
|
if (!is_local_addr(&conn->base_.addr)) {
|
||||||
/* Don't report the source address for a nearby/private connection.
|
/* Don't report the source address for a nearby/private connection.
|
||||||
* Otherwise we tend to mis-report in cases where incoming ports are
|
* Otherwise we tend to mis-report in cases where incoming ports are
|
||||||
* being forwarded to a Tor server running behind the firewall. */
|
* being forwarded to a Tor server running behind the firewall. */
|
||||||
tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
|
buf_add_printf(buf, X_ADDRESS_HEADER "%s\r\n", conn->base_.address);
|
||||||
X_ADDRESS_HEADER "%s\r\n", conn->base_.address);
|
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (encoding) {
|
if (encoding) {
|
||||||
tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
|
buf_add_printf(buf, "Content-Encoding: %s\r\n", encoding);
|
||||||
"Content-Encoding: %s\r\n", encoding);
|
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (length >= 0) {
|
if (length >= 0) {
|
||||||
tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
|
buf_add_printf(buf, "Content-Length: %ld\r\n", (long)length);
|
||||||
"Content-Length: %ld\r\n", (long)length);
|
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (cache_lifetime > 0) {
|
if (cache_lifetime > 0) {
|
||||||
char expbuf[RFC1123_TIME_LEN+1];
|
char expbuf[RFC1123_TIME_LEN+1];
|
||||||
format_rfc1123_time(expbuf, (time_t)(now + cache_lifetime));
|
format_rfc1123_time(expbuf, (time_t)(now + cache_lifetime));
|
||||||
/* We could say 'Cache-control: max-age=%d' here if we start doing
|
/* We could say 'Cache-control: max-age=%d' here if we start doing
|
||||||
* http/1.1 */
|
* http/1.1 */
|
||||||
tor_snprintf(cp, sizeof(tmp)-(cp-tmp),
|
buf_add_printf(buf, "Expires: %s\r\n", expbuf);
|
||||||
"Expires: %s\r\n", expbuf);
|
|
||||||
cp += strlen(cp);
|
|
||||||
} else if (cache_lifetime == 0) {
|
} else if (cache_lifetime == 0) {
|
||||||
/* We could say 'Cache-control: no-cache' here if we start doing
|
/* We could say 'Cache-control: no-cache' here if we start doing
|
||||||
* http/1.1 */
|
* http/1.1 */
|
||||||
strlcpy(cp, "Pragma: no-cache\r\n", sizeof(tmp)-(cp-tmp));
|
buf_add_string(buf, "Pragma: no-cache\r\n");
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (extra_headers) {
|
if (extra_headers) {
|
||||||
strlcpy(cp, extra_headers, sizeof(tmp)-(cp-tmp));
|
buf_add_string(buf, extra_headers);
|
||||||
cp += strlen(cp);
|
|
||||||
}
|
}
|
||||||
if (sizeof(tmp)-(cp-tmp) > 3)
|
buf_add_string(buf, "\r\n");
|
||||||
memcpy(cp, "\r\n", 3);
|
|
||||||
else
|
connection_buf_add_buf(TO_CONN(conn), buf);
|
||||||
tor_assert(0);
|
buf_free(buf);
|
||||||
connection_buf_add(tmp, strlen(tmp), TO_CONN(conn));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** As write_http_response_header_impl, but sets encoding and content-typed
|
/** As write_http_response_header_impl, but sets encoding and content-typed
|
||||||
|
Loading…
Reference in New Issue
Block a user