int http_req_prepare(http_req *a_req) { int l_return = 0; char l_buf[30]; if (!a_req) return -1; memset(l_buf, 0, 30); /* set the host header */ http_hdr_set_value(a_req->headers, http_hdr_Host, a_req->host); /* check to see if we have an entity body */ if ((a_req->type == http_req_type_post) || (a_req->type == http_req_type_put) || (a_req->type == http_req_type_trace)) { sprintf(l_buf, "%d", a_req->body_len); http_hdr_set_value(a_req->headers, http_hdr_Content_Length, l_buf); } /* if the user agent isn't set then set a default */ if (http_hdr_get_value(a_req->headers, http_hdr_User_Agent) == NULL) http_hdr_set_value(a_req->headers, http_hdr_User_Agent, "libghttp/1.0"); return l_return; }
int ghttp_prepare(ghttp_request *a_request) { /* only allow http requests if no proxy has been set */ if (!a_request->proxy->host && a_request->uri->proto && strcmp(a_request->uri->proto, "http")) return -1; /* check to see if we have to set up the host information */ if ((a_request->conn->host == NULL) || (a_request->conn->host != a_request->uri->host) || (a_request->conn->port != a_request->uri->port) || (a_request->conn->proxy_host != a_request->proxy->host) || (a_request->conn->proxy_port != a_request->proxy->port)) { /* reset everything. */ a_request->conn->host = a_request->uri->host; a_request->req->host = a_request->uri->host; a_request->req->full_uri = a_request->uri->full; a_request->conn->port = a_request->uri->port; a_request->conn->proxy_host = a_request->proxy->host; a_request->conn->proxy_port = a_request->proxy->port; a_request->conn->hostinfo = NULL; /* close the socket if it looks open */ if (a_request->conn->sock >= 0) { close(a_request->conn->sock); a_request->conn->sock = -1; a_request->connected = 0; } } /* check to see if we need to change the resource. */ if ((a_request->req->resource == NULL) || (a_request->req->resource != a_request->uri->resource)) { a_request->req->resource = a_request->uri->resource; a_request->req->host = a_request->uri->host; } /* set the authorization header */ if ((a_request->authtoken != NULL) && (strlen(a_request->authtoken) > 0)) { http_hdr_set_value(a_request->req->headers, http_hdr_Authorization, a_request->authtoken); } else { http_hdr_set_value(a_request->req->headers, http_hdr_WWW_Authenticate, NULL); } /* set the proxy authorization header */ if ((a_request->proxy_authtoken != NULL) && (strlen(a_request->proxy_authtoken) > 0)) { http_hdr_set_value(a_request->req->headers, http_hdr_Proxy_Authorization, a_request->proxy_authtoken); } http_req_prepare(a_request->req); return 0; }
void ghttp_set_header(ghttp_request *a_request, const char *a_hdr, const char *a_val) { http_hdr_set_value(a_request->req->headers, a_hdr, a_val); }
int http_hdr_set_value_no_nts(http_hdr_list *a_list, const char *a_name_start, int a_name_len, const char *a_val_start, int a_val_len) { int l_return = 0; char *l_temp_name = NULL; char *l_temp_val = NULL; /* note that a zero len value is valid... */ if ((a_list == NULL) || (a_name_start == NULL) || (a_val_start == NULL) || (a_name_len == 0)) goto ec; l_temp_name = (char *)malloc(a_name_len + 1); memset(l_temp_name, 0, a_name_len + 1); memcpy(l_temp_name, a_name_start, a_name_len); l_temp_val = (char *)malloc(a_val_len + 1); memset(l_temp_val, 0, a_val_len + 1); memcpy(l_temp_val, a_val_start, a_val_len); /* set the value */ l_return = http_hdr_set_value(a_list, l_temp_name, l_temp_val); free(l_temp_name); free(l_temp_val); ec: return l_return; }
int ghttp_prepare(ghttp_request *a_request) { /* only allow http requests if no proxy has been set */ if (!a_request->proxy->host && a_request->uri->proto && strcmp(a_request->uri->proto, "http")) return 1; /* check to see if we have to set up the host information */ if ((a_request->conn->host == NULL) || (a_request->conn->host != a_request->uri->host) || (a_request->conn->port != a_request->uri->port) || (a_request->conn->proxy_host != a_request->proxy->host) || (a_request->conn->proxy_port != a_request->proxy->port)) { /* reset everything. */ a_request->conn->host = a_request->uri->host; a_request->req->host = a_request->uri->host; a_request->req->full_uri = a_request->uri->full; a_request->req->port = a_request->uri->port; a_request->conn->port = a_request->uri->port; a_request->conn->proxy_host = a_request->proxy->host; a_request->conn->proxy_port = a_request->proxy->port; a_request->conn->hostinfo = NULL; /* close the socket if it looks open */ #ifdef WIN32 //windows if (a_request->conn->sock != INVALID_SOCKET) { closesocket(a_request->conn->sock); a_request->conn->sock = INVALID_SOCKET; #else //linux if (a_request->conn->sock >= 0) { close(a_request->conn->sock); a_request->conn->sock = -1; #endif a_request->connected = 0; } } /* check to see if we need to change the resource. */ if ((a_request->req->resource == NULL) || (a_request->req->resource != a_request->uri->resource)) { a_request->req->resource = a_request->uri->resource; a_request->req->host = a_request->uri->host; a_request->req->port = a_request->uri->port; } /* set the authorization header */ if ((a_request->authtoken != NULL) && (strlen(a_request->authtoken) > 0)) { http_hdr_set_value(a_request->req->headers, http_hdr_Authorization, a_request->authtoken); } else { http_hdr_set_value(a_request->req->headers, http_hdr_WWW_Authenticate, NULL); } /* set the proxy authorization header */ if ((a_request->proxy_authtoken != NULL) && (strlen(a_request->proxy_authtoken) > 0)) { http_hdr_set_value(a_request->req->headers, http_hdr_Proxy_Authorization, a_request->proxy_authtoken); } http_req_prepare(a_request->req); return 0; } ghttp_status ghttp_send(ghttp_request *a_request) { int l_rv = 0; if (a_request->connected == 0) { if (http_trans_connect(a_request->conn) < 0) { if (a_request->conn->error_type == http_trans_err_type_errno) a_request->errstr = strerror(a_request->conn->error); else if(a_request->conn->error_type == http_trans_err_type_host) a_request->errstr = http_trans_get_host_error(h_errno); return ghttp_error; } a_request->connected = 1; } l_rv = http_req_send(a_request->req, a_request->conn); if (l_rv == HTTP_TRANS_ERR) return ghttp_error; if (l_rv == HTTP_TRANS_NOT_DONE) return ghttp_not_done; if (l_rv == HTTP_TRANS_DONE) { if (a_request->conn->sync == HTTP_TRANS_ASYNC) return ghttp_not_done; } return ghttp_error; }