Newer
Older
Daniel Stenberg
committed
contains body data */
Daniel Stenberg
committed
int socketindex)
ssize_t amount;
CURLcode res;
char *ptr;
Daniel Stenberg
committed
struct HTTP *http = conn->data->state.proto.http;
Daniel Stenberg
committed
curl_socket_t sockfd;
size_t headersize;
Daniel Stenberg
committed
DEBUGASSERT(socketindex <= SECONDARYSOCKET);
Daniel Stenberg
committed
sockfd = conn->sock[socketindex];
Daniel Stenberg
committed
/* The looping below is required since we use non-blocking sockets, but due
to the circumstances we will just loop and try again and again etc */
ptr = in->buffer;
size = in->size_used;
headersize = size - included_body_bytes; /* the initial part that isn't body
is header */
DEBUGASSERT(size > included_body_bytes);
Daniel Stenberg
committed
#ifdef CURL_DOES_CONVERSIONS
res = Curl_convert_to_network(conn->data, ptr, headersize);
Daniel Stenberg
committed
/* Curl_convert_to_network calls failf if unsuccessful */
if(res != CURLE_OK) {
/* conversion failed, free memory and return to the caller */
if(in->buffer)
free(in->buffer);
free(in);
return res;
}
#endif /* CURL_DOES_CONVERSIONS */
if(conn->protocol & PROT_HTTPS) {
/* We never send more than CURL_MAX_WRITE_SIZE bytes in one single chunk
when we speak HTTPS, as if only a fraction of it is sent now, this data
needs to fit into the normal read-callback buffer later on and that
buffer is using this size.
*/
sendsize= (size > CURL_MAX_WRITE_SIZE)?CURL_MAX_WRITE_SIZE:size;
/* OpenSSL is very picky and we must send the SAME buffer pointer to the
library when we attempt to re-send this buffer. Sending the same data
is not enough, we must use the exact same address. For this reason, we
must copy the data to the uploadbuffer first, since that is the buffer
we will be using if this send is retried later.
*/
memcpy(conn->data->state.uploadbuffer, ptr, sendsize);
ptr = conn->data->state.uploadbuffer;
}
else
sendsize = size;
res = Curl_write(conn, sockfd, ptr, sendsize, &amount);
Daniel Stenberg
committed
if(CURLE_OK == res) {
/*
* Note that we may not send the entire chunk at once, and we have a set
* number of data bytes at the end of the big buffer (out of which we may
* only send away a part).
*/
/* how much of the header that was sent */
size_t headlen = (size_t)amount>headersize?headersize:(size_t)amount;
size_t bodylen = amount - headlen;
if(conn->data->set.verbose) {
/* this data _may_ contain binary stuff */
Curl_debug(conn->data, CURLINFO_HEADER_OUT, ptr, headlen, conn);
if((size_t)amount > headlen) {
/* there was body data sent beyond the initial header part, pass that
on to the debug callback too */
Daniel Stenberg
committed
Curl_debug(conn->data, CURLINFO_DATA_OUT,
ptr+headlen, bodylen, conn);
}
if(bodylen)
Daniel Stenberg
committed
/* since we sent a piece of the body here, up the byte counter for it
accordingly */
http->writebytecount += bodylen;
*bytes_written += amount;
if(http) {
if((size_t)amount != size) {
/* The whole request could not be sent in one system call. We must
queue it up and send it later when we get the chance. We must not
loop here and wait until it might work again. */
Daniel Stenberg
committed
ptr = in->buffer + amount;
/* backup the currently set pointers */
http->backup.fread_func = conn->fread_func;
http->backup.fread_in = conn->fread_in;
http->backup.postdata = http->postdata;
http->backup.postsize = http->postsize;
Daniel Stenberg
committed
/* set the new pointers for the request-sending */
conn->fread_func = (curl_read_callback)readmoredata;
conn->fread_in = (void *)conn;
http->postdata = ptr;
http->postsize = (curl_off_t)size;
Daniel Stenberg
committed
http->send_buffer = in;
http->sending = HTTPSEND_REQUEST;
return CURLE_OK;
}
http->sending = HTTPSEND_BODY;
/* the full buffer was sent, clean up and return */
}
else {
if((size_t)amount != size)
/* We have no continue-send mechanism now, fail. This can only happen
when this function is used from the CONNECT sending function. We
currently (stupidly) assume that the whole request is always sent
away in the first single chunk.
This needs FIXing.
*/
return CURLE_SEND_ERROR;
else
conn->writechannel_inuse = FALSE;
}
Daniel Stenberg
committed
}
if(in->buffer)
free(in->buffer);
free(in);
return res;
}
/*
* add_bufferf() add the formatted input to the buffer.
*/
static
CURLcode add_bufferf(send_buffer *in, const char *fmt, ...)
{
char *s;
va_list ap;
va_start(ap, fmt);
s = vaprintf(fmt, ap); /* this allocs a new string to append */
va_end(ap);
if(s) {
CURLcode result = add_buffer(in, s, strlen(s));
free(s);
/* If we failed, we cleanup the whole buffer and return error */
if(in->buffer)
free(in->buffer);
free(in);
return CURLE_OUT_OF_MEMORY;
}
/*
* add_buffer() appends a memory chunk to the existing buffer
*/
static
CURLcode add_buffer(send_buffer *in, const void *inptr, size_t size)
{
char *new_rb;
if(~size < in->size_used) {
/* If resulting used size of send buffer would wrap size_t, cleanup
the whole buffer and return error. Otherwise the required buffer
size will fit into a single allocatable memory chunk */
Curl_safefree(in->buffer);
free(in);
return CURLE_OUT_OF_MEMORY;
}
if(!in->buffer ||
((in->size_used + size) > (in->size_max - 1))) {
/* If current buffer size isn't enough to hold the result, use a
buffer size that doubles the required size. If this new size
would wrap size_t, then just use the largest possible one */
if((size > (size_t)-1/2) || (in->size_used > (size_t)-1/2) ||
(~(size*2) < (in->size_used*2)))
new_size = (size_t)-1;
else
new_size = (in->size_used+size)*2;
if(in->buffer)
/* we have a buffer, enlarge the existing one */
new_rb = realloc(in->buffer, new_size);
else
/* create a new buffer */
if(!new_rb) {
/* If we failed, we cleanup the whole buffer and return error */
Curl_safefree(in->buffer);
free(in);
return CURLE_OUT_OF_MEMORY;
in->buffer = new_rb;
in->size_max = new_size;
memcpy(&in->buffer[in->size_used], inptr, size);
in->size_used += size;
return CURLE_OK;
}
/* end of the add_buffer functions */
/* ------------------------------------------------------------------------- */
/*
* Curl_compareheader()
*
* Returns TRUE if 'headerline' contains the 'header' with given 'content'.
* Pass headers WITH the colon.
*/
bool
Curl_compareheader(const char *headerline, /* line to check */
const char *header, /* header keyword _with_ colon */
const char *content) /* content string to find */
{
/* RFC2616, section 4.2 says: "Each header field consists of a name followed
* by a colon (":") and the field value. Field names are case-insensitive.
* The field value MAY be preceded by any amount of LWS, though a single SP
* is preferred." */
size_t hlen = strlen(header);
size_t clen;
size_t len;
const char *start;
const char *end;
if(!Curl_raw_nequal(headerline, header, hlen))
return FALSE; /* doesn't start with header */
/* pass the header */
start = &headerline[hlen];
/* pass all white spaces */
Daniel Stenberg
committed
while(*start && ISSPACE(*start))
start++;
/* find the end of the header line */
end = strchr(start, '\r'); /* lines end with CRLF */
if(!end) {
/* in case there's a non-standard compliant line here */
end = strchr(start, '\n');
if(!end)
/* hm, there's no line ending here, use the zero byte! */
end = strchr(start, '\0');
}
len = end-start; /* length of the content part of the input line */
clen = strlen(content); /* length of the word to find */
/* find the content string in the rest of the line */
for(;len>=clen;len--, start++) {
if(Curl_raw_nequal(start, content, clen))
return TRUE; /* match! */
}
return FALSE; /* no match */
}
#ifndef CURL_DISABLE_PROXY
Daniel Stenberg
committed
* Curl_proxyCONNECT() requires that we're connected to a HTTP proxy. This
* function will issue the necessary commands to get a seamless tunnel through
* this proxy. After that, the socket can be used just as a normal socket.
*
* This badly needs to be rewritten. CONNECT should be sent and dealt with
* like any ordinary HTTP request, and not specially crafted like this. This
* function only remains here like this for now since the rewrite is a bit too
* much work to do at the moment.
Daniel Stenberg
committed
*
* This function is BLOCKING which is nasty for all multi interface using apps.
Daniel Stenberg
committed
CURLcode Curl_proxyCONNECT(struct connectdata *conn,
int sockindex,
const char *hostname,
unsigned short remote_port)
{
int subversion=0;
Daniel Stenberg
committed
struct SessionHandle *data=conn->data;
Daniel Stenberg
committed
struct SingleRequest *k = &data->req;
Daniel Stenberg
committed
CURLcode result;
int res;
Daniel Stenberg
committed
long timeout =
data->set.timeout?data->set.timeout:PROXY_TIMEOUT; /* in milliseconds */
curl_socket_t tunnelsocket = conn->sock[sockindex];
Daniel Stenberg
committed
curl_off_t cl=0;
Daniel Stenberg
committed
bool closeConnection = FALSE;
Daniel Stenberg
committed
bool chunked_encoding = FALSE;
Daniel Stenberg
committed
long check;
#define SELECT_OK 0
#define SELECT_ERROR 1
#define SELECT_TIMEOUT 2
int error = SELECT_OK;
Daniel Stenberg
committed
conn->bits.proxy_connect_closed = FALSE;
do {
Daniel Stenberg
committed
if(!conn->bits.tunnel_connecting) { /* BEGIN CONNECT PHASE */
Daniel Stenberg
committed
char *host_port;
send_buffer *req_buffer;
infof(data, "Establish HTTP proxy tunnel to %s:%d\n",
hostname, remote_port);
Daniel Stenberg
committed
if(data->req.newurl) {
Daniel Stenberg
committed
/* This only happens if we've looped here due to authentication
reasons, and we don't really use the newly cloned URL here
then. Just free() it. */
Daniel Stenberg
committed
free(data->req.newurl);
data->req.newurl = NULL;
Daniel Stenberg
committed
}
Daniel Stenberg
committed
/* initialize a dynamic send-buffer */
req_buffer = add_buffer_init();
Daniel Stenberg
committed
if(!req_buffer)
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
host_port = aprintf("%s:%d", hostname, remote_port);
if(!host_port) {
free(req_buffer);
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
Daniel Stenberg
committed
/* Setup the proxy-authorization header, if any */
result = http_output_auth(conn, "CONNECT", host_port, TRUE);
if(CURLE_OK == result) {
Daniel Stenberg
committed
char *host=(char *)"";
const char *proxyconn="";
const char *useragent="";
const char *http = (conn->proxytype == CURLPROXY_HTTP_1_0) ?
"1.0" : "1.1";
Daniel Stenberg
committed
if(!checkheaders(data, "Host:")) {
host = aprintf("Host: %s\r\n", host_port);
if(!host) {
free(req_buffer);
free(host_port);
Daniel Stenberg
committed
}
if(!checkheaders(data, "Proxy-Connection:"))
proxyconn = "Proxy-Connection: Keep-Alive\r\n";
Daniel Stenberg
committed
if(!checkheaders(data, "User-Agent:") &&
data->set.str[STRING_USERAGENT])
Daniel Stenberg
committed
useragent = conn->allocptr.uagent;
Daniel Stenberg
committed
/* Send the connect request to the proxy */
/* BLOCKING */
result =
add_bufferf(req_buffer,
"CONNECT %s:%d HTTP/%s\r\n"
Daniel Stenberg
committed
"%s" /* Host: */
"%s" /* Proxy-Authorization */
"%s" /* User-Agent */
"%s", /* Proxy-Connection */
hostname, remote_port, http,
Daniel Stenberg
committed
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
host,
conn->allocptr.proxyuserpwd?
conn->allocptr.proxyuserpwd:"",
useragent,
proxyconn);
if(host && *host)
free(host);
if(CURLE_OK == result)
result = add_custom_headers(conn, req_buffer);
if(CURLE_OK == result)
/* CRLF terminate the request */
result = add_bufferf(req_buffer, "\r\n");
if(CURLE_OK == result) {
/* Now send off the request */
result = add_buffer_send(req_buffer, conn,
&data->info.request_size, 0, sockindex);
}
Daniel Stenberg
committed
if(result)
failf(data, "Failed sending CONNECT to proxy");
Daniel Stenberg
committed
free(host_port);
if(result)
Daniel Stenberg
committed
return result;
Daniel Stenberg
committed
conn->bits.tunnel_connecting = TRUE;
} /* END CONNECT PHASE */
Daniel Stenberg
committed
/* now we've issued the CONNECT and we're waiting to hear back -
we try not to block here in multi-mode because that might be a LONG
wait if the proxy cannot connect-through to the remote host. */
Daniel Stenberg
committed
/* if timeout is requested, find out how much remaining time we have */
check = timeout - /* timeout time */
Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */
Daniel Stenberg
committed
if(check <= 0) {
Daniel Stenberg
committed
failf(data, "Proxy CONNECT aborted due to timeout");
Daniel Stenberg
committed
return CURLE_RECV_ERROR;
Daniel Stenberg
committed
}
Daniel Stenberg
committed
/* if we're in multi-mode and we would block, return instead for a retry */
Daniel Stenberg
committed
if(Curl_if_multi == data->state.used_interface) {
if(0 == Curl_socket_ready(tunnelsocket, CURL_SOCKET_BAD, 0))
Daniel Stenberg
committed
/* return so we'll be called again polling-style */
return CURLE_OK;
else {
DEBUGF(infof(data,
"Multi mode finished polling for response from "
"proxy CONNECT."));
}
Daniel Stenberg
committed
}
else {
DEBUGF(infof(data, "Easy mode waiting for response from proxy CONNECT."));
}
Daniel Stenberg
committed
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
/* at this point, either:
1) we're in easy-mode and so it's okay to block waiting for a CONNECT
response
2) we're in multi-mode and we didn't block - it's either an error or we
now have some data waiting.
In any case, the tunnel_connecting phase is over. */
conn->bits.tunnel_connecting = FALSE;
{ /* BEGIN NEGOTIATION PHASE */
size_t nread; /* total size read */
int perline; /* count bytes per line */
int keepon=TRUE;
ssize_t gotbytes;
char *ptr;
char *line_start;
ptr=data->state.buffer;
line_start = ptr;
nread=0;
perline=0;
keepon=TRUE;
while((nread<BUFSIZE) && (keepon && !error)) {
/* if timeout is requested, find out how much remaining time we have */
check = timeout - /* timeout time */
Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */
if(check <= 0) {
failf(data, "Proxy CONNECT aborted due to timeout");
error = SELECT_TIMEOUT; /* already too little time */
break;
}
Daniel Stenberg
committed
/* loop every second at least, less if the timeout is near */
switch (Curl_socket_ready(tunnelsocket, CURL_SOCKET_BAD,
Daniel Stenberg
committed
check<1000L?(int)check:1000)) {
case -1: /* select() error, stop reading */
error = SELECT_ERROR;
failf(data, "Proxy CONNECT aborted due to select/poll error");
Daniel Stenberg
committed
break;
case 0: /* timeout */
break;
default:
DEBUGASSERT(ptr+BUFSIZE-nread <= data->state.buffer+BUFSIZE+1);
Daniel Stenberg
committed
res = Curl_read(conn, tunnelsocket, ptr, BUFSIZE-nread, &gotbytes);
if(res< 0)
/* EWOULDBLOCK */
continue; /* go loop yourself */
else if(res)
keepon = FALSE;
else if(gotbytes <= 0) {
keepon = FALSE;
if(data->set.proxyauth && data->state.authproxy.avail) {
/* proxy auth was requested and there was proxy auth available,
then deem this as "mere" proxy disconnect */
conn->bits.proxy_connect_closed = TRUE;
}
else {
error = SELECT_ERROR;
failf(data, "Proxy CONNECT aborted");
}
Daniel Stenberg
committed
}
else {
/*
* We got a whole chunk of data, which can be anything from one
* byte to a set of lines and possibly just a piece of the last
* line.
*/
int i;
nread += gotbytes;
if(keepon > TRUE) {
Daniel Stenberg
committed
/* This means we are currently ignoring a response-body */
Daniel Stenberg
committed
nread = 0; /* make next read start over in the read buffer */
ptr=data->state.buffer;
Daniel Stenberg
committed
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
if(cl) {
/* A Content-Length based body: simply count down the counter
and make sure to break out of the loop when we're done! */
cl -= gotbytes;
if(cl<=0) {
keepon = FALSE;
break;
}
}
else {
/* chunked-encoded body, so we need to do the chunked dance
properly to know when the end of the body is reached */
CHUNKcode r;
ssize_t tookcareof=0;
/* now parse the chunked piece of data so that we can
properly tell when the stream ends */
r = Curl_httpchunk_read(conn, ptr, gotbytes, &tookcareof);
if(r == CHUNKE_STOP) {
/* we're done reading chunks! */
infof(data, "chunk reading DONE\n");
keepon = FALSE;
}
else
infof(data, "Read %d bytes of chunk, continue\n",
tookcareof);
}
}
Daniel Stenberg
committed
else
for(i = 0; i < gotbytes; ptr++, i++) {
perline++; /* amount of bytes in this line so far */
Daniel Stenberg
committed
char letter;
int writetype;
#ifdef CURL_DOES_CONVERSIONS
/* convert from the network encoding */
result = Curl_convert_from_network(data, line_start, perline);
/* Curl_convert_from_network calls failf if unsuccessful */
if(result)
return result;
#endif /* CURL_DOES_CONVERSIONS */
Daniel Stenberg
committed
/* output debug if that is requested */
if(data->set.verbose)
Curl_debug(data, CURLINFO_HEADER_IN,
line_start, (size_t)perline, conn);
/* send the header to the callback */
writetype = CLIENTWRITE_HEADER;
if(data->set.include_header)
writetype |= CLIENTWRITE_BODY;
Daniel Stenberg
committed
result = Curl_client_write(conn, writetype, line_start,
perline);
Daniel Stenberg
committed
if(result)
return result;
/* Newlines are CRLF, so the CR is ignored as the line isn't
really terminated until the LF comes. Treat a following CR
as end-of-headers as well.*/
if(('\r' == line_start[0]) ||
('\n' == line_start[0])) {
/* end of response-headers from the proxy */
Daniel Stenberg
committed
nread = 0; /* make next read start over in the read
buffer */
ptr=data->state.buffer;
Daniel Stenberg
committed
if((407 == k->httpcode) && !data->state.authproblem) {
Daniel Stenberg
committed
/* If we get a 407 response code with content length
Daniel Stenberg
committed
when we have no auth problem, we must ignore the
whole response-body */
Daniel Stenberg
committed
keepon = 2;
Daniel Stenberg
committed
if(cl) {
infof(data, "Ignore %" FORMAT_OFF_T
Daniel Stenberg
committed
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
" bytes of response-body\n", cl);
/* remove the remaining chunk of what we already
read */
cl -= (gotbytes - i);
if(cl<=0)
/* if the whole thing was already read, we are done!
*/
keepon=FALSE;
}
else if(chunked_encoding) {
CHUNKcode r;
/* We set ignorebody true here since the chunked
decoder function will acknowledge that. Pay
attention so that this is cleared again when this
function returns! */
k->ignorebody = TRUE;
infof(data, "%d bytes of chunk left\n", gotbytes-i);
if(line_start[1] == '\n') {
/* this can only be a LF if the letter at index 0
was a CR */
line_start++;
i++;
}
/* now parse the chunked piece of data so that we can
properly tell when the stream ends */
r = Curl_httpchunk_read(conn, line_start+1,
gotbytes -i, &gotbytes);
if(r == CHUNKE_STOP) {
/* we're done reading chunks! */
infof(data, "chunk reading DONE\n");
keepon = FALSE;
}
else
infof(data, "Read %d bytes of chunk, continue\n",
gotbytes);
}
else {
/* without content-length or chunked encoding, we
can't keep the connection alive since the close is
the end signal so we bail out at once instead */
Daniel Stenberg
committed
keepon=FALSE;
Daniel Stenberg
committed
}
Daniel Stenberg
committed
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
}
else
keepon = FALSE;
break; /* breaks out of for-loop, not switch() */
}
/* keep a backup of the position we are about to blank */
letter = line_start[perline];
line_start[perline]=0; /* zero terminate the buffer */
if((checkprefix("WWW-Authenticate:", line_start) &&
(401 == k->httpcode)) ||
(checkprefix("Proxy-authenticate:", line_start) &&
(407 == k->httpcode))) {
result = Curl_http_input_auth(conn, k->httpcode,
line_start);
if(result)
return result;
}
else if(checkprefix("Content-Length:", line_start)) {
cl = curlx_strtoofft(line_start + strlen("Content-Length:"),
NULL, 10);
}
else if(Curl_compareheader(line_start,
"Connection:", "close"))
closeConnection = TRUE;
Daniel Stenberg
committed
else if(Curl_compareheader(line_start,
"Transfer-Encoding:", "chunked")) {
infof(data, "CONNECT responded chunked\n");
chunked_encoding = TRUE;
/* init our chunky engine */
Curl_httpchunk_init(conn);
}
Daniel Stenberg
committed
else if(Curl_compareheader(line_start,
"Proxy-Connection:", "close"))
closeConnection = TRUE;
Daniel Stenberg
committed
else if(2 == sscanf(line_start, "HTTP/1.%d %d",
&subversion,
&k->httpcode)) {
/* store the HTTP code from the proxy */
data->info.httpproxycode = k->httpcode;
}
/* put back the letter we blanked out before */
line_start[perline]= letter;
perline=0; /* line starts over here */
line_start = ptr+1; /* this skips the zero byte we wrote */
}
}
}
Daniel Stenberg
committed
break;
} /* switch */
if(Curl_pgrsUpdate(conn))
return CURLE_ABORTED_BY_CALLBACK;
Daniel Stenberg
committed
} /* while there's buffer left and loop is requested */
if(error)
return CURLE_RECV_ERROR;
Daniel Stenberg
committed
if(data->info.httpproxycode != 200) {
Daniel Stenberg
committed
/* Deal with the possibly already received authenticate
headers. 'newurl' is set to a new URL if we must loop. */
Daniel Stenberg
committed
result = Curl_http_auth_act(conn);
if(result)
return result;
if(conn->bits.close)
/* the connection has been marked for closure, most likely in the
Curl_http_auth_act() function and thus we can kill it at once
below
*/
closeConnection = TRUE;
}
Daniel Stenberg
committed
Daniel Stenberg
committed
if(closeConnection && data->req.newurl) {
Daniel Stenberg
committed
/* Connection closed by server. Don't use it anymore */
sclose(conn->sock[sockindex]);
conn->sock[sockindex] = CURL_SOCKET_BAD;
break;
Daniel Stenberg
committed
}
} /* END NEGOTIATION PHASE */
Daniel Stenberg
committed
} while(data->req.newurl);
Daniel Stenberg
committed
if(200 != data->req.httpcode) {
failf(data, "Received HTTP code %d from proxy after CONNECT",
Daniel Stenberg
committed
data->req.httpcode);
Daniel Stenberg
committed
Daniel Stenberg
committed
if(closeConnection && data->req.newurl)
Daniel Stenberg
committed
conn->bits.proxy_connect_closed = TRUE;
return CURLE_RECV_ERROR;
Daniel Stenberg
committed
/* If a proxy-authorization header was used for the proxy, then we should
make sure that it isn't accidentally used for the document request
after we've connected. So let's free and clear it here. */
Curl_safefree(conn->allocptr.proxyuserpwd);
conn->allocptr.proxyuserpwd = NULL;
data->state.authproxy.done = TRUE;
infof (data, "Proxy replied OK to CONNECT request\n");
Daniel Stenberg
committed
data->req.ignorebody = FALSE; /* put it (back) to non-ignore state */
return CURLE_OK;
}
#endif /* CURL_DISABLE_PROXY */
* Curl_http_connect() performs HTTP stuff to do at connect-time, called from
* the generic Curl_connect().
CURLcode Curl_http_connect(struct connectdata *conn, bool *done)
Daniel Stenberg
committed
struct SessionHandle *data;
Daniel Stenberg
committed
/* We default to persistent connections. We set this already in this connect
function to make the re-use checks properly be able to check this bit. */
conn->bits.close = FALSE;
#ifndef CURL_DISABLE_PROXY
/* If we are not using a proxy and we want a secure connection, perform SSL
* initialization & connection now. If using a proxy with https, then we
* must tell the proxy to CONNECT to the host we want to talk to. Only
* after the connect has occurred, can we start talking SSL
Daniel Stenberg
committed
if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
/* either SSL over proxy, or explicitly asked for */
Daniel Stenberg
committed
result = Curl_proxyCONNECT(conn, FIRSTSOCKET,
conn->host.name,
conn->remote_port);
if(CURLE_OK != result)
return result;
}
Daniel Stenberg
committed
if(conn->bits.tunnel_connecting) {
Daniel Stenberg
committed
/* nothing else to do except wait right now - we're not done here. */
return CURLE_OK;
}
#endif /* CURL_DISABLE_PROXY */
Daniel Stenberg
committed
Daniel Stenberg
committed
if(conn->protocol & PROT_HTTPS) {
/* perform SSL initialization */
if(data->state.used_interface == Curl_if_multi) {
result = https_connecting(conn, done);
Daniel Stenberg
committed
if(result)
return result;
}
else {
/* BLOCKING */
result = Curl_ssl_connect(conn, FIRSTSOCKET);
if(result)
return result;
*done = TRUE;
}
}
else {
*done = TRUE;
}
Daniel Stenberg
committed
Daniel Stenberg
committed
/* this returns the socket to wait for in the DO and DOING state for the multi
interface and then we're always _sending_ a request and thus we wait for
the single socket to become writable only */
static int http_getsock_do(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
/* write mode */
(void)numsocks; /* unused, we trust it to be at least 1 */
socks[0] = conn->sock[FIRSTSOCKET];
return GETSOCK_WRITESOCK(0);
}
static CURLcode https_connecting(struct connectdata *conn, bool *done)
Daniel Stenberg
committed
{
CURLcode result;
DEBUGASSERT((conn) && (conn->protocol & PROT_HTTPS));
Daniel Stenberg
committed
/* perform SSL initialization for this socket */
result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, done);
conn->bits.close = TRUE; /* a failed connection is marked for closure
to prevent (bad) re-use or similar */
Daniel Stenberg
committed
}
Daniel Stenberg
committed
#ifdef USE_SSLEAY
Daniel Stenberg
committed
/* This function is OpenSSL-specific. It should be made to query the generic
SSL layer instead. */
static int https_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
Daniel Stenberg
committed
{
Daniel Stenberg
committed
if(conn->protocol & PROT_HTTPS) {
Daniel Stenberg
committed
struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
Daniel Stenberg
committed
if(!numsocks)
return GETSOCK_BLANK;
Daniel Stenberg
committed
Daniel Stenberg
committed
if(connssl->connecting_state == ssl_connect_2_writing) {
Daniel Stenberg
committed
/* write mode */
Daniel Stenberg
committed
socks[0] = conn->sock[FIRSTSOCKET];
return GETSOCK_WRITESOCK(0);
Daniel Stenberg
committed
}
Daniel Stenberg
committed
else if(connssl->connecting_state == ssl_connect_2_reading) {
Daniel Stenberg
committed
/* read mode */
Daniel Stenberg
committed
socks[0] = conn->sock[FIRSTSOCKET];
return GETSOCK_READSOCK(0);
Daniel Stenberg
committed
}
}
return CURLE_OK;
}
Daniel Stenberg
committed
#else
#ifdef USE_GNUTLS
static int https_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
Daniel Stenberg
committed
{
(void)conn;
(void)socks;
(void)numsocks;
return GETSOCK_BLANK;
}
#else
#ifdef USE_NSS
static int https_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
(void)conn;
(void)socks;
(void)numsocks;
return GETSOCK_BLANK;
}
#else
#ifdef USE_QSOSSL
static int https_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
(void)conn;
(void)socks;
(void)numsocks;
return GETSOCK_BLANK;
}
#endif
#endif
Daniel Stenberg
committed
#endif
Daniel Stenberg
committed
#endif
/*
* Curl_http_done() gets called from Curl_done() after a single HTTP request
* has been performed.
*/
CURLcode Curl_http_done(struct connectdata *conn,
Daniel Stenberg
committed
CURLcode status, bool premature)
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
struct HTTP *http =data->state.proto.http;
Daniel Stenberg
committed
(void)premature; /* not used */
Daniel Stenberg
committed
Curl_unencode_cleanup(conn);
/* set the proper values (possibly modified on POST) */
conn->fread_func = data->set.fread_func; /* restore */
conn->fread_in = data->set.in; /* restore */
Daniel Stenberg
committed
conn->seek_func = data->set.seek_func; /* restore */
conn->seek_client = data->set.seek_client; /* restore */
Daniel Stenberg
committed
if(http == NULL)
return CURLE_OK;
Daniel Stenberg
committed
if(http->send_buffer) {
send_buffer *buff = http->send_buffer;
Daniel Stenberg
committed
free(buff->buffer);
free(buff);
http->send_buffer = NULL; /* clear the pointer */
Daniel Stenberg
committed
}
Daniel Stenberg
committed
if(HTTPREQ_POST_FORM == data->set.httpreq) {
Daniel Stenberg
committed
data->req.bytecount = http->readbytecount + http->writebytecount;
Curl_formclean(&http->sendit); /* Now free that whole lot */
/* a file being uploaded was left opened, close it! */
fclose(http->form.fp);
http->form.fp = NULL;
}
else if(HTTPREQ_PUT == data->set.httpreq)
Daniel Stenberg
committed
data->req.bytecount = http->readbytecount + http->writebytecount;
Daniel Stenberg
committed
if(status != CURLE_OK)
if(!premature && /* this check is pointless when DONE is called before the
entire operation is complete */
!conn->bits.retry &&
((http->readbytecount +
Daniel Stenberg
committed
data->req.headerbytecount -
data->req.deductheadercount)) <= 0) {
/* If this connection isn't simply closed to be retried, AND nothing was
read from the HTTP server (that counts), this can't be right so we
return an error here */
failf(data, "Empty reply from server");
Daniel Stenberg
committed
return CURLE_GOT_NOTHING;
}
/* Determine if we should use HTTP 1.1 for this request. Reasons to avoid it
are if the user specifically requested HTTP 1.0, if the server we are
connected to only supports 1.0, or if any server previously contacted to
handle this request only supports 1.0. */
static bool use_http_1_1(const struct SessionHandle *data,
const struct connectdata *conn)
{
return (bool)((data->set.httpversion == CURL_HTTP_VERSION_1_1) ||
((data->set.httpversion != CURL_HTTP_VERSION_1_0) &&
((conn->httpversion == 11) ||
((conn->httpversion != 10) &&
}
/* check and possibly add an Expect: header */
static CURLcode expect100(struct SessionHandle *data,
struct connectdata *conn,
send_buffer *req_buffer)
{
CURLcode result = CURLE_OK;
Daniel Stenberg
committed
data->state.expect100header = FALSE; /* default to false unless it is set
to TRUE below */
if(use_http_1_1(data, conn) && !checkheaders(data, "Expect:")) {
/* if not doing HTTP 1.0 or disabled explicitly, we add a Expect:
100-continue to the headers which actually speeds up post
operations (as there is one packet coming back from the web
server) */
result = add_bufferf(req_buffer,
"Expect: 100-continue\r\n");
if(result == CURLE_OK)
Daniel Stenberg
committed
data->state.expect100header = TRUE;
}
return result;
}
static CURLcode add_custom_headers(struct connectdata *conn,
send_buffer *req_buffer)