Skip to content

Commit

Permalink
http: Create add_common_headers() to simplify HTTP request code
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Oct 28, 2012
1 parent 26f752c commit bcab63e
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions http.c
Expand Up @@ -423,10 +423,27 @@ static int process_http_response(struct openconnect_info *vpninfo, int *result,
return done;
}

static void add_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
struct vpn_option *opt;

buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
buf_append(buf, "Accept: */*\r\n");
buf_append(buf, "Accept-Encoding: identity\r\n");

if (vpninfo->cookies) {
buf_append(buf, "Cookie: ");
for (opt = vpninfo->cookies; opt; opt = opt->next)
buf_append(buf, "%s=%s%s", opt->option,
opt->value, opt->next ? "; " : "\r\n");
}
buf_append(buf, "X-Transcend-Version: 1\r\n");
}

static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
char *server_sha1)
{
struct vpn_option *opt;
struct oc_text_buf *buf;
char *config_buf = NULL;
int result, buflen;
Expand All @@ -443,18 +460,8 @@ static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,

buf = buf_alloc();
buf_append(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
buf_append(buf, "Accept: */*\r\n");
buf_append(buf, "Accept-Encoding: identity\r\n");

if (vpninfo->cookies) {
buf_append(buf, "Cookie: ");
for (opt = vpninfo->cookies; opt; opt = opt->next)
buf_append(buf, "%s=%s%s", opt->option,
opt->value, opt->next ? "; " : "\r\n");
}
buf_append(buf, "X-Transcend-Version: 1\r\n\r\n");
add_common_headers(vpninfo, buf);
buf_append(buf, "\r\n");

if (buf_error(buf))
return buf_free(buf);
Expand Down Expand Up @@ -819,22 +826,14 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
*/
buf = buf_alloc();
buf_append(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
buf_append(buf, "Accept: */*\r\n");
buf_append(buf, "Accept-Encoding: identity\r\n");
add_common_headers(vpninfo, buf);

if (vpninfo->cookies) {
buf_append(buf, "Cookie: ");
for (opt = vpninfo->cookies; opt; opt = opt->next)
buf_append(buf, "%s=%s%s", opt->option,
opt->value, opt->next ? "; " : "\r\n");
}
if (request_body_type) {
buf_append(buf, "Content-Type: %s\r\n", request_body_type);
buf_append(buf, "Content-Length: %zd\r\n", strlen(request_body));
}
buf_append(buf, "X-Transcend-Version: 1\r\n\r\n");
buf_append(buf, "\r\n");

if (request_body_type)
buf_append(buf, "%s", request_body);

Expand Down

0 comments on commit bcab63e

Please sign in to comment.