-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_response_encode.c
30 lines (24 loc) · 1011 Bytes
/
http_response_encode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h> /* snprintf() */
#include "httpkit/http_response_encode.h"
#include "http_header_encode.h"
#include "def.h"
#define MAX_CODE_LEN 5
int http_response_encode_status_line(struct qbuf* res, unsigned int code,
const char* text, unsigned int text_len) {
char code_str[MAX_CODE_LEN + 1];
int code_len = snprintf(code_str, MAX_CODE_LEN + 1, " %u ", code);
int ret = qbuf_reserve(res, HTTP_VERSION_STR_LEN + text_len + MAX_CODE_LEN +
2 /* "\r\n" */);
if (ret != 0) {
return HRC_NOMEM;
}
qbuf_append(res, HTTP_VERSION_STR, HTTP_VERSION_STR_LEN);
qbuf_append(res, code_str, code_len);
qbuf_append(res, text, text_len);
qbuf_append(res, "\r\n", 2);
return HRC_OK;
}
int http_response_encode_header(struct qbuf* res, const char* key, unsigned int klen,
const char* value, unsigned int vlen) {
return http_header_encode(res, key, klen, value, vlen);
}