void http_req_ok(const http_req_t* req, const char* type) { char buf[1024]; strcpy(buf, "HTTP/1.0 200 OK\r\n"); http_req_send(req, buf); sprintf(buf, "%s\r\n", req->source->name); http_req_send(req, buf); sprintf(buf, "Content-Type: %s\r\n", type); http_req_send(req, buf); strcpy(buf, "\r\n"); http_req_send(req, buf); }
void http_req_not_found(const http_req_t* req) { char buf[1024]; strcpy(buf, "HTTP/1.0 404 NOT FOUND\r\n"); http_req_send(req, buf); sprintf(buf, "%s\r\n", req->source->name); http_req_send(req, buf); strcpy(buf, "Content-Type: text/html\r\n"); http_req_send(req, buf); strcpy(buf, "\r\n"); http_req_send(req, buf); strcpy(buf, "<html><head><title>Not Found</title></head>\r\n"); http_req_send(req, buf); strcpy(buf, "<body><p>The server could not fulfill\r\n"); http_req_send(req, buf); strcpy(buf, "your request because the resource specified\r\n"); http_req_send(req, buf); strcpy(buf, "is unavailable or nonexistant.</p></body></html>\r\n"); http_req_send(req, buf); }
void http_req_not_implemented(const http_req_t* req) { char buf[1024]; strcpy(buf, "HTTP/1.0 501 METHOD NOT IMPLEMENTED\r\n"); http_req_send(req, buf); sprintf(buf, "%s\r\n", req->source->name); http_req_send(req, buf); strcpy(buf, "Content-Type: text/html\r\n"); http_req_send(req, buf); strcpy(buf, "\r\n"); http_req_send(req, buf); strcpy(buf, "<html><head><title>Method Not Implemented</title></head>\r\n"); http_req_send(req, buf); strcpy(buf, "<body><p>The server does not recognize the requested Method</p></body></html>\r\n"); http_req_send(req, buf); }
ghttp_status ghttp_process(ghttp_request *a_request) { int l_rv = 0; ghttpDebug("=======proc: %d \n", a_request->proc); if(a_request->proc == ghttp_proc_done) return ghttp_done; if (a_request->proc == ghttp_proc_none) a_request->proc = ghttp_proc_request; if (a_request->proc == ghttp_proc_request) { if (a_request->connected == 0) { ghttpDebug("connect ... \n"); 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; } ghttpDebug("send request ... \n"); l_rv = http_req_send(a_request->req, a_request->conn); if (l_rv == HTTP_TRANS_ERR) { a_request->errstr = a_request->conn->errstr ? a_request->conn->errstr : GHTTP_ERROR_TRANS_SEND; return ghttp_error; } if (l_rv == HTTP_TRANS_NOT_DONE) return ghttp_not_done; if (l_rv == HTTP_TRANS_DONE) { a_request->proc = ghttp_proc_response_hdrs; if (a_request->conn->sync == HTTP_TRANS_ASYNC) return ghttp_not_done; } } if (a_request->proc == ghttp_proc_response_hdrs) { ghttpDebug("read headers ... \n"); l_rv = http_resp_read_headers(a_request->resp, a_request->conn); if (l_rv == HTTP_TRANS_ERR) { a_request->errstr = a_request->conn->errstr ? a_request->conn->errstr : GHTTP_ERROR_TRANS_READ_HEAD; return ghttp_error; } if (l_rv == HTTP_TRANS_NOT_DONE) return ghttp_not_done; if (l_rv == HTTP_TRANS_DONE) { a_request->proc = ghttp_proc_response; if (a_request->conn->sync == HTTP_TRANS_ASYNC) return ghttp_not_done; } } if (a_request->proc == ghttp_proc_response) { ghttpDebug("read body ... \n"); l_rv = http_resp_read_body(a_request->resp, a_request->req, a_request->conn); if (l_rv == HTTP_TRANS_ERR) { a_request->errstr = a_request->conn->errstr ? a_request->conn->errstr : GHTTP_ERROR_TRANS_READ_BODY; /* make sure that the connected flag is fixed and stuff */ if (a_request->conn->sock == -1) a_request->connected = 0; return ghttp_error; } if (l_rv == HTTP_TRANS_NEXT) return ghttp_next; if (l_rv == HTTP_TRANS_NOT_DONE) return ghttp_not_done; if (l_rv == HTTP_TRANS_DONE) { /* make sure that the connected flag is fixed and stuff */ if (a_request->conn->sock == -1) a_request->connected = 0; a_request->proc = ghttp_proc_done; return ghttp_done; } } return ghttp_error; }
ghttp_status ghttp_process(ghttp_request *a_request) { int l_rv = 0; if (a_request->proc == ghttp_proc_none) a_request->proc = ghttp_proc_request; if (a_request->proc == ghttp_proc_request) { 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) { a_request->proc = ghttp_proc_response_hdrs; if (a_request->conn->sync == HTTP_TRANS_ASYNC) return ghttp_not_done; } } if (a_request->proc == ghttp_proc_response_hdrs) { l_rv = http_resp_read_headers(a_request->resp, 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) { a_request->proc = ghttp_proc_response; if (a_request->conn->sync == HTTP_TRANS_ASYNC) return ghttp_not_done; } } if (a_request->proc == ghttp_proc_response) { l_rv = http_resp_read_body(a_request->resp, a_request->req, a_request->conn); if (l_rv == HTTP_TRANS_ERR) { /* make sure that the connected flag is fixed and stuff */ if (a_request->conn->sock == -1) a_request->connected = 0; return ghttp_error; } if (l_rv == HTTP_TRANS_NOT_DONE) return ghttp_not_done; if (l_rv == HTTP_TRANS_DONE) { /* make sure that the connected flag is fixed and stuff */ if (a_request->conn->sock == -1) a_request->connected = 0; a_request->proc = ghttp_proc_none; return ghttp_done; } } return ghttp_error; }
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; }