forked from baidu/dperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
106 lines (92 loc) · 3.04 KB
/
http.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2021 Baidu.com, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Jianzhang Peng (pengjianzhang@baidu.com)
*/
#include "http.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "version.h"
/*
* Don't Change HTTP1.1 Request/Response Format.
* Keep the minimum request and response packet sizes the same.
* Mimimal HTTP/1.1 Packet Size is 128 Bytes.
* */
#define HTTP_REQ_FORMAT \
"GET %s HTTP/1.1\r\n" \
"User-Agent: dperf\r\n" \
"Host: %s\r\n" \
"Accept: */*\r\n" \
"P: aa\r\n" \
"\r\n"
#define HTTP_RSP_FORMAT \
"HTTP/1.1 200 OK\n" \
"Serv:dperf\n" \
"Content-Length:%4d\n" \
"Connection:keep-alive\n" \
"\n" \
"%s"
static char http_rsp[MBUF_DATA_SIZE];
static char http_req[MBUF_DATA_SIZE];
static const char *http_rsp_body_default = "hello dperf!\r\n";
const char *http_get_request(void)
{
return http_req;
}
const char *http_get_response(void)
{
return http_rsp;
}
static void http_set_payload_client(struct config *cfg, char *dest, int len, int payload_size)
{
int pad = 0;
char buf[MBUF_DATA_SIZE] = {0};
if (payload_size <= 0) {
snprintf(dest, len, HTTP_REQ_FORMAT, cfg->http_path, cfg->http_host);
} else if (payload_size < HTTP_DATA_MIN_SIZE) {
config_set_payload(cfg, dest, payload_size, 1);
} else {
pad = payload_size - HTTP_DATA_MIN_SIZE;
if (pad > 0) {
config_set_payload(cfg, buf, pad, 0);
}
buf[0] = '/';
snprintf(dest, len, HTTP_REQ_FORMAT, buf, cfg->http_host);
}
}
static void http_set_payload_server(struct config *cfg, char *dest, int len, int payload_size)
{
int pad = 0;
char buf[MBUF_DATA_SIZE] = {0};
const char *data = NULL;
if (payload_size <= 0) {
data = http_rsp_body_default;
snprintf(dest, len, HTTP_RSP_FORMAT, (int)strlen(data), data);
} else if (payload_size < HTTP_DATA_MIN_SIZE) {
config_set_payload(cfg, dest, payload_size, 1);
} else {
pad = payload_size - HTTP_DATA_MIN_SIZE;
if (pad > 0) {
config_set_payload(cfg, buf, pad, 1);
}
snprintf(dest, len, HTTP_RSP_FORMAT, (int)strlen(buf), buf);
}
}
void http_set_payload(struct config *cfg, int payload_size)
{
http_set_payload_server(cfg, http_rsp, MBUF_DATA_SIZE, payload_size);
http_set_payload_client(cfg, http_req, MBUF_DATA_SIZE, payload_size);
}