ghttp_request * ghttp_request_new(void) { struct _ghttp_request *l_return = NULL; /* create everything */ l_return = malloc(sizeof(struct _ghttp_request)); memset(l_return, 0, sizeof(struct _ghttp_request)); l_return->uri = http_uri_new(); l_return->proxy = http_uri_new(); l_return->req = http_req_new(); l_return->resp = http_resp_new(); l_return->conn = http_trans_conn_new(); return l_return; }
int ghttp_set_uri(ghttp_request *a_request, char *a_uri) { int l_rv = 0; http_uri *l_new_uri = NULL; if ((!a_request) || (!a_uri)) return -1; /* set the uri */ l_new_uri = http_uri_new(); l_rv = http_uri_parse(a_uri, l_new_uri); if (l_rv < 0) { http_uri_destroy(l_new_uri); return -1; } if (a_request->uri) { /* check to see if this has been set yet. */ if (a_request->uri->host && a_request->uri->port && a_request->uri->resource) { /* check to see if we just need to change the resource */ if ((!strcmp(a_request->uri->host, l_new_uri->host)) && (a_request->uri->port == l_new_uri->port)) { free(a_request->uri->resource); /* make a copy since we're about to destroy it */ a_request->uri->resource = strdup(l_new_uri->resource); http_uri_destroy(l_new_uri); } else { goto REPLACE_URI; } } else { goto REPLACE_URI; } } else a_request->uri = l_new_uri; return 0; REPLACE_URI: http_uri_destroy(a_request->uri); a_request->uri = l_new_uri; return 0; }
static int http_register_uri(struct http_server *hs, const char *uri, void (*handler_cb)(evutil_socket_t fd, short events, void *arg)) { struct http_uri *hu = http_uri_new(uri, handler_cb); if (!hu) { printf("Could not register uri %s\n", uri); return -1; } struct http_uri *prev = NULL; if (hs->uris == NULL) { hs->uris = hu; } else { prev = hs->uris; while (prev->next != NULL) { prev = prev->next; } prev->next = hu; } return 0; }
void ghttp_request_destroy(ghttp_request *a_request) { if (!a_request) return; /* make sure that the socket was shut down. */ #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 } /* destroy everything else */ if (a_request->uri) http_uri_destroy(a_request->uri); if (a_request->proxy) http_uri_destroy(a_request->proxy); if (a_request->req) http_req_destroy(a_request->req); if (a_request->resp) http_resp_destroy(a_request->resp); if (a_request->conn) http_trans_conn_destroy(a_request->conn); /* destroy username info. */ if (a_request->username) { free(a_request->username); a_request->username = NULL; } if (a_request->password) { free(a_request->password); a_request->password = NULL; } if (a_request->authtoken) { free(a_request->authtoken); a_request->authtoken = NULL; } /* destroy proxy authentication */ if (a_request->proxy_username) { free(a_request->proxy_username); a_request->proxy_username = NULL; } if (a_request->proxy_password) { free(a_request->proxy_password); a_request->proxy_password = NULL; } if (a_request->proxy_authtoken) { free(a_request->proxy_authtoken); a_request->proxy_authtoken = NULL; } if (a_request) free(a_request); return; } int ghttp_uri_validate(char *a_uri) { if (!a_uri) return -1; /* you can do this... */ return(http_uri_parse(a_uri, NULL)); } int ghttp_set_uri(ghttp_request *a_request, char *a_uri) { int l_rv = 0; http_uri *l_new_uri = NULL; if ((!a_request) || (!a_uri)) return -1; /* set the uri */ l_new_uri = http_uri_new(); l_rv = http_uri_parse(a_uri, l_new_uri); if (l_rv < 0) { http_uri_destroy(l_new_uri); return -1; } if (a_request->uri) { /* check to see if this has been set yet. */ if (a_request->uri->host && a_request->uri->port && a_request->uri->resource) { /* check to see if we just need to change the resource */ if ((!strcmp(a_request->uri->host, l_new_uri->host)) && (a_request->uri->port == l_new_uri->port)) { free(a_request->uri->resource); /* make a copy since we're about to destroy it */ a_request->uri->resource = strdup(l_new_uri->resource); http_uri_destroy(l_new_uri); } else { http_uri_destroy(a_request->uri); a_request->uri = l_new_uri; } } else { http_uri_destroy(a_request->uri); a_request->uri = l_new_uri; } } return 0; }