Add support for arbitrary http requests

Submitted by:	Alex Hornung <alex@alexhornung.com>
Reviewed by:	des
Obtained from:	Dragonfly
MFC after:	3 week
This commit is contained in:
Baptiste Daroussin 2014-06-05 22:16:26 +00:00
parent 4bd8c06c3a
commit c41991303c
3 changed files with 37 additions and 2 deletions

View File

@ -117,6 +117,9 @@ int fetch_no_proxy_match(const char *);
*/
FILE *http_request(struct url *, const char *,
struct url_stat *, struct url *, const char *);
FILE *http_request_body(struct url *, const char *,
struct url_stat *, struct url *, const char *,
const char *, const char *);
FILE *ftp_request(struct url *, const char *,
struct url_stat *, struct url *, const char *);

View File

@ -102,6 +102,8 @@ FILE *fetchGetHTTP(struct url *, const char *);
FILE *fetchPutHTTP(struct url *, const char *);
int fetchStatHTTP(struct url *, struct url_stat *, const char *);
struct url_ent *fetchListHTTP(struct url *, const char *);
FILE *fetchReqHTTP(struct url *, const char *, const char *,
const char *, const char *);
/* FTP-specific functions */
FILE *fetchXGetFTP(struct url *, struct url_stat *, const char *);

View File

@ -1494,6 +1494,14 @@ http_print_html(FILE *out, FILE *in)
* Core
*/
FILE *
http_request(struct url *URL, const char *op, struct url_stat *us,
struct url *purl, const char *flags)
{
return (http_request_body(URL, op, us, purl, flags, NULL, NULL));
}
/*
* Send a request and process the reply
*
@ -1501,8 +1509,9 @@ http_print_html(FILE *out, FILE *in)
* XXX off into a separate function.
*/
FILE *
http_request(struct url *URL, const char *op, struct url_stat *us,
struct url *purl, const char *flags)
http_request_body(struct url *URL, const char *op, struct url_stat *us,
struct url *purl, const char *flags, const char *content_type,
const char *body)
{
char timebuf[80];
char hbuf[MAXHOSTNAMELEN + 7], *host;
@ -1519,6 +1528,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
http_headerbuf_t headerbuf;
http_auth_challenges_t server_challenges;
http_auth_challenges_t proxy_challenges;
size_t body_len;
/* The following calls don't allocate anything */
init_http_headerbuf(&headerbuf);
@ -1695,8 +1705,19 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
if (url->offset > 0)
http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
http_cmd(conn, "Connection: close");
if (body) {
body_len = strlen(body);
http_cmd(conn, "Content-Length: %zu", body_len);
if (content_type != NULL)
http_cmd(conn, "Content-Type: %s", content_type);
}
http_cmd(conn, "");
if (body)
fetch_write(conn, body, body_len);
/*
* Force the queued request to be dispatched. Normally, one
* would do this with shutdown(2) but squid proxies can be
@ -2047,3 +2068,12 @@ fetchListHTTP(struct url *url __unused, const char *flags __unused)
warnx("fetchListHTTP(): not implemented");
return (NULL);
}
FILE *
fetchReqHTTP(struct url *URL, const char *method, const char *flags,
const char *content_type, const char *body)
{
return (http_request_body(URL, method, NULL, http_get_proxy(URL, flags),
flags, content_type, body));
}