return &req->custHeaders[HeaderIndex];
}
-typedef enum {
- READMODE_SYNC,
- READMODE_ASYNC,
- READMODE_NOBLOCK
-} read_mode_t;
-
struct data_stream_vtbl_t {
DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
BOOL (*end_of_data)(data_stream_t*,http_request_t*);
- DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,read_mode_t);
+ DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
BOOL (*drain_content)(data_stream_t*,http_request_t*);
void (*destroy)(data_stream_t*);
};
DWORD buf_size;
DWORD buf_pos;
DWORD chunk_size;
+ BOOL end_of_data;
} chunked_stream_t;
static inline void destroy_data_stream(data_stream_t *stream)
static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
{
gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
- return gzip_stream->end_of_data;
+ return gzip_stream->end_of_data
+ || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
}
static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
- DWORD *read, read_mode_t read_mode)
+ DWORD *read, blocking_mode_t blocking_mode)
{
gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
z_stream *zstream = &gzip_stream->zstream;
DWORD current_read, ret_read = 0;
- BOOL end;
int zres;
DWORD res = ERROR_SUCCESS;
- while(size && !gzip_stream->end_of_data) {
- end = gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req);
+ TRACE("(%d %d)\n", size, blocking_mode);
- if(gzip_stream->buf_size <= 64 && !end) {
+ while(size && !gzip_stream->end_of_data) {
+ if(!gzip_stream->buf_size) {
if(gzip_stream->buf_pos) {
if(gzip_stream->buf_size)
memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
gzip_stream->buf_pos = 0;
}
res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
- sizeof(gzip_stream->buf)-gzip_stream->buf_size, ¤t_read, read_mode);
+ sizeof(gzip_stream->buf)-gzip_stream->buf_size, ¤t_read, blocking_mode);
gzip_stream->buf_size += current_read;
if(res != ERROR_SUCCESS)
break;
- end = gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req);
- if(!current_read && !end) {
- if(read_mode != READMODE_NOBLOCK) {
+
+ if(!current_read) {
+ if(blocking_mode != BLOCKING_DISALLOW) {
WARN("unexpected end of data\n");
gzip_stream->end_of_data = TRUE;
}
break;
}
- if(gzip_stream->buf_size <= 64 && !end)
- continue;
}
zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
- zstream->avail_in = gzip_stream->buf_size-(end ? 0 : 64);
+ zstream->avail_in = gzip_stream->buf_size;
zstream->next_out = buf+ret_read;
zstream->avail_out = size;
zres = inflate(&gzip_stream->zstream, 0);
break;
}
- if(ret_read && read_mode == READMODE_ASYNC)
- read_mode = READMODE_NOBLOCK;
+ if(ret_read && blocking_mode == BLOCKING_ALLOW)
+ blocking_mode = BLOCKING_DISALLOW;
}
TRACE("read %u bytes\n", ret_read);
heap_free(address);
}
-static DWORD init_gzip_stream(http_request_t *req)
+static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
{
gzip_stream_t *gzip_stream;
int index, zres;
gzip_stream->zstream.zalloc = wininet_zalloc;
gzip_stream->zstream.zfree = wininet_zfree;
- zres = inflateInit2(&gzip_stream->zstream, 0x1f);
+ zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
if(zres != Z_OK) {
ERR("inflateInit failed: %d\n", zres);
heap_free(gzip_stream);
#else
-static DWORD init_gzip_stream(http_request_t *req)
+static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
{
ERR("gzip stream not supported, missing zlib.\n");
return ERROR_SUCCESS;
{
LPWSTR szVerb = NULL, szObjectName = NULL;
LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
- HINTERNET rc = FALSE;
+ HINTERNET rc = NULL;
TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
TRACE("\n");
- if(request->hCacheFile) {
+ if(request->hCacheFile)
CloseHandle(request->hCacheFile);
- DeleteFileW(request->cacheFile);
- }
- heap_free(request->cacheFile);
+ if(request->req_file)
+ req_file_release(request->req_file);
request->read_section.DebugInfo->Spare[0] = 0;
DeleteCriticalSection( &request->read_section );
static void http_release_netconn(http_request_t *req, BOOL reuse)
{
- TRACE("%p %p\n",req, req->netconn);
+ TRACE("%p %p %x\n",req, req->netconn, reuse);
- if(!req->netconn)
+ if(!is_valid_netconn(req->netconn))
return;
#ifndef __REACTOS__
INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
- free_netconn(req->netconn);
- req->netconn = NULL;
+ close_netconn(req->netconn);
INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
info->Flags |= IDSI_FLAG_KEEP_ALIVE;
if (req->proxy)
info->Flags |= IDSI_FLAG_PROXY;
- if (req->netconn && req->netconn->secure)
+ if (is_valid_netconn(req->netconn) && req->netconn->secure)
info->Flags |= IDSI_FLAG_SECURE;
return ERROR_SUCCESS;
return ERROR_INSUFFICIENT_BUFFER;
*size = sizeof(DWORD);
- flags = req->netconn ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
+ flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
*(DWORD *)buffer = flags;
TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
- if(!req->cacheFile) {
+ if(!req->req_file) {
*size = 0;
return ERROR_INTERNET_ITEM_NOT_FOUND;
}
if(unicode) {
- req_size = (lstrlenW(req->cacheFile)+1) * sizeof(WCHAR);
+ req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
if(*size < req_size)
return ERROR_INSUFFICIENT_BUFFER;
*size = req_size;
- memcpy(buffer, req->cacheFile, *size);
+ memcpy(buffer, req->req_file->file_name, *size);
return ERROR_SUCCESS;
}else {
- req_size = WideCharToMultiByte(CP_ACP, 0, req->cacheFile, -1, NULL, 0, NULL, NULL);
+ req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
if (req_size > *size)
return ERROR_INSUFFICIENT_BUFFER;
- *size = WideCharToMultiByte(CP_ACP, 0, req->cacheFile,
+ *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
-1, buffer, *size, NULL, NULL);
return ERROR_SUCCESS;
}
TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
flags &= SECURITY_SET_MASK;
req->security_flags |= flags;
- if(req->netconn)
+ if(is_valid_netconn(req->netconn))
req->netconn->security_flags |= flags;
return ERROR_SUCCESS;
}
if(HTTP_GetRequestURL(req, url)) {
WCHAR *header;
DWORD header_len;
+ BOOL res;
header = build_response_header(req, TRUE);
header_len = (header ? strlenW(header) : 0);
- CommitUrlCacheEntryW(url, req->cacheFile, req->expires,
+ res = CommitUrlCacheEntryW(url, req->req_file->file_name, req->expires,
req->last_modified, NORMAL_CACHE_ENTRY,
header, header_len, NULL, 0);
+ if(res)
+ req->req_file->is_committed = TRUE;
+ else
+ WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
heap_free(header);
}
}
BOOL b = TRUE;
/* FIXME: We should free previous cache file earlier */
- heap_free(req->cacheFile);
- CloseHandle(req->hCacheFile);
- req->hCacheFile = NULL;
+ if(req->req_file) {
+ req_file_release(req->req_file);
+ req->req_file = NULL;
+ }
+ if(req->hCacheFile) {
+ CloseHandle(req->hCacheFile);
+ req->hCacheFile = NULL;
+ }
if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
b = FALSE;
return;
}
- req->cacheFile = heap_strdupW(file_name);
- req->hCacheFile = CreateFileW(req->cacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
+ create_req_file(file_name, &req->req_file);
+
+ req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(req->hCacheFile == INVALID_HANDLE_VALUE) {
WARN("Could not create file: %u\n", GetLastError());
if (maxlen == -1) maxlen = sizeof(req->read_buf);
res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
- maxlen - req->read_size, 0, &len );
+ maxlen - req->read_size, BLOCKING_ALLOW, &len );
if(res == ERROR_SUCCESS)
req->read_size += len;
return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
}
-static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, read_mode_t read_mode)
+static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
{
DWORD res;
- res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, read_mode);
+ res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
assert(*read <= size);
if(req->hCacheFile) {
}
/* fetch some more data into the read buffer (the read section must be held) */
-static DWORD refill_read_buffer(http_request_t *req, read_mode_t read_mode, DWORD *read_bytes)
+static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
{
DWORD res, read=0;
}
res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
- &read, read_mode);
+ &read, blocking_mode);
req->read_size += read;
TRACE("read %u bytes, read_size %u\n", read, req->read_size);
netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
DWORD avail = 0;
- if(req->netconn)
+ if(is_valid_netconn(req->netconn))
NETCON_query_data_available(req->netconn, &avail);
return netconn_stream->content_length == ~0u
? avail
static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
{
netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
- return netconn_stream->content_read == netconn_stream->content_length || !req->netconn;
+ return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
}
static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
- DWORD *read, read_mode_t read_mode)
+ DWORD *read, blocking_mode_t blocking_mode)
{
netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
DWORD res = ERROR_SUCCESS;
size = min(size, netconn_stream->content_length-netconn_stream->content_read);
- if(read_mode == READMODE_NOBLOCK) {
- DWORD avail = netconn_get_avail_data(stream, req);
- if (size > avail)
- size = avail;
- }
-
- if(size && req->netconn) {
- if((res = NETCON_recv(req->netconn, buf, size, read_mode == READMODE_SYNC ? MSG_WAITALL : 0, &len)))
+ if(size && is_valid_netconn(req->netconn)) {
+ if((res = NETCON_recv(req->netconn, buf, size, blocking_mode, &len))) {
len = 0;
- if(!len)
+ if(blocking_mode == BLOCKING_DISALLOW && res == WSAEWOULDBLOCK)
+ res = ERROR_SUCCESS;
+ else
+ netconn_stream->content_length = netconn_stream->content_read;
+ }else if(!len) {
netconn_stream->content_length = netconn_stream->content_read;
+ }
}
netconn_stream->content_read += *read = len;
{
netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
BYTE buf[1024];
- DWORD avail;
int len;
if(netconn_end_of_data(stream, req))
return TRUE;
do {
- avail = netconn_get_avail_data(stream, req);
- if(!avail)
- return FALSE;
-
- if(NETCON_recv(req->netconn, buf, min(avail, sizeof(buf)), 0, &len) != ERROR_SUCCESS)
+ if(NETCON_recv(req->netconn, buf, sizeof(buf), BLOCKING_DISALLOW, &len) != ERROR_SUCCESS)
return FALSE;
netconn_stream->content_read += len;
DWORD res;
int len;
+ assert(!stream->end_of_data);
+
if (stream->buf_pos)
{
/* move existing data to the start of the buffer */
if (maxlen == -1) maxlen = sizeof(stream->buf);
res = NETCON_recv( req->netconn, stream->buf + stream->buf_size,
- maxlen - stream->buf_size, 0, &len );
+ maxlen - stream->buf_size, BLOCKING_ALLOW, &len );
if(res == ERROR_SUCCESS)
stream->buf_size += len;
/* read the size of the next chunk (the read section must be held) */
static DWORD start_next_chunk(chunked_stream_t *stream, http_request_t *req)
{
- /* TODOO */
DWORD chunk_size = 0, res;
- if(stream->chunk_size != ~0u && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
+ assert(!stream->chunk_size || stream->chunk_size == ~0u);
+
+ if (stream->end_of_data) return ERROR_SUCCESS;
+
+ /* read terminator for the previous chunk */
+ if(!stream->chunk_size && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
return res;
for (;;)
{
TRACE( "reading %u byte chunk\n", chunk_size );
stream->chunk_size = chunk_size;
- req->contentLength += chunk_size;
+ if (req->contentLength == ~0u) req->contentLength = chunk_size;
+ else req->contentLength += chunk_size;
+
+ if (!chunk_size) stream->end_of_data = TRUE;
return discard_chunked_eol(stream, req);
}
remove_chunked_data(stream, 1);
static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
{
chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
- return !chunked_stream->chunk_size;
+ return chunked_stream->end_of_data;
}
static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
- DWORD *read, read_mode_t read_mode)
+ DWORD *read, blocking_mode_t blocking_mode)
{
chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
DWORD read_bytes = 0, ret_read = 0, res = ERROR_SUCCESS;
- if(chunked_stream->chunk_size == ~0u) {
+ if(!chunked_stream->chunk_size || chunked_stream->chunk_size == ~0u) {
res = start_next_chunk(chunked_stream, req);
if(res != ERROR_SUCCESS)
return res;
}
- while(size && chunked_stream->chunk_size) {
+ while(size && chunked_stream->chunk_size && !chunked_stream->end_of_data) {
if(chunked_stream->buf_size) {
read_bytes = min(size, min(chunked_stream->buf_size, chunked_stream->chunk_size));
/* this could block */
- if(read_mode == READMODE_NOBLOCK && read_bytes == chunked_stream->chunk_size)
+ if(blocking_mode == BLOCKING_DISALLOW && read_bytes == chunked_stream->chunk_size)
break;
memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
}else {
read_bytes = min(size, chunked_stream->chunk_size);
- if(read_mode == READMODE_NOBLOCK) {
+ if(blocking_mode == BLOCKING_DISALLOW) {
DWORD avail;
- if(!req->netconn || !NETCON_query_data_available(req->netconn, &avail) || !avail)
+ if(!is_valid_netconn(req->netconn) || !NETCON_query_data_available(req->netconn, &avail) || !avail)
break;
if(read_bytes > avail)
read_bytes = avail;
break;
}
- res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, 0, (int*)&read_bytes);
+ res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, BLOCKING_ALLOW, (int*)&read_bytes);
if(res != ERROR_SUCCESS)
break;
}
chunked_stream->chunk_size -= read_bytes;
size -= read_bytes;
ret_read += read_bytes;
- if(!chunked_stream->chunk_size) {
- assert(read_mode != READMODE_NOBLOCK);
+ if(size && !chunked_stream->chunk_size) {
+ assert(blocking_mode != BLOCKING_DISALLOW);
res = start_next_chunk(chunked_stream, req);
if(res != ERROR_SUCCESS)
break;
}
- if(read_mode == READMODE_ASYNC)
- read_mode = READMODE_NOBLOCK;
+ if(blocking_mode == BLOCKING_ALLOW)
+ blocking_mode = BLOCKING_DISALLOW;
}
TRACE("read %u bytes\n", ret_read);
{
chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
- /* FIXME: we can do better */
- return !chunked_stream->chunk_size;
+ remove_chunked_data(chunked_stream, chunked_stream->buf_size);
+ return chunked_stream->end_of_data;
}
static void chunked_destroy(data_stream_t *stream)
chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
chunked_stream->buf_size = chunked_stream->buf_pos = 0;
chunked_stream->chunk_size = ~0u;
+ chunked_stream->end_of_data = FALSE;
if(request->read_size) {
memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
if(request->decoding) {
int encoding_idx;
+ static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
static const WCHAR gzipW[] = {'g','z','i','p',0};
encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
- if(encoding_idx != -1 && !strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
- HTTP_DeleteCustomHeader(request, encoding_idx);
- return init_gzip_stream(request);
+ if(encoding_idx != -1) {
+ if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
+ HTTP_DeleteCustomHeader(request, encoding_idx);
+ return init_gzip_stream(request, TRUE);
+ }
+ if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
+ HTTP_DeleteCustomHeader(request, encoding_idx);
+ return init_gzip_stream(request, FALSE);
+ }
}
}
static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
{
DWORD res, read = 0, avail = 0;
- read_mode_t mode;
+ blocking_mode_t mode;
TRACE("%p\n", req);
EnterCriticalSection( &req->read_section );
- mode = first_notif && req->read_size ? READMODE_NOBLOCK : READMODE_ASYNC;
+ mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
res = refill_read_buffer(req, mode, &read);
if(res == ERROR_SUCCESS)
avail = get_avail_data(req);
LeaveCriticalSection( &req->read_section );
- if(res != ERROR_SUCCESS || (mode != READMODE_NOBLOCK && !read)) {
+ if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
WARN("res %u read %u, closing connection\n", res, read);
http_release_netconn(req, FALSE);
}
static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
{
DWORD current_read = 0, ret_read = 0;
- read_mode_t read_mode;
+ blocking_mode_t blocking_mode;
DWORD res = ERROR_SUCCESS;
- read_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? READMODE_ASYNC : READMODE_SYNC;
+ blocking_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? BLOCKING_ALLOW : BLOCKING_WAITALL;
EnterCriticalSection( &req->read_section );
memcpy(buffer, req->read_buf+req->read_pos, ret_read);
req->read_size -= ret_read;
req->read_pos += ret_read;
- if(read_mode == READMODE_ASYNC)
- read_mode = READMODE_NOBLOCK;
+ if(blocking_mode == BLOCKING_ALLOW)
+ blocking_mode = BLOCKING_DISALLOW;
}
if(ret_read < size) {
- res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, ¤t_read, read_mode);
+ res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, ¤t_read, blocking_mode);
ret_read += current_read;
}
{
BOOL ret;
- if(!req->netconn || req->contentLength == -1)
+ if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
return FALSE;
if(!strcmpW(req->verb, szHEAD))
/* never wait, if we can't enter the section we queue an async request right away */
if (TryEnterCriticalSection( &req->read_section ))
{
- refill_read_buffer(req, READMODE_NOBLOCK, NULL);
+ refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
if ((*available = get_avail_data( req ))) goto done;
if (end_of_read_data( req )) goto done;
LeaveCriticalSection( &req->read_section );
if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
{
- refill_read_buffer( req, READMODE_ASYNC, NULL );
+ refill_read_buffer( req, BLOCKING_ALLOW, NULL );
*available = get_avail_data( req );
}
return ERROR_SUCCESS;
}
+static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
+{
+ http_request_t *req = (http_request_t*)hdr;
+
+ TRACE("(%p)\n", req);
+
+ if(!req->req_file) {
+ WARN("No cache file name available\n");
+ return ERROR_FILE_NOT_FOUND;
+ }
+
+ *ret = req_file_addref(req->req_file);
+ return ERROR_SUCCESS;
+}
+
static const object_vtbl_t HTTPREQVtbl = {
HTTPREQ_Destroy,
HTTPREQ_CloseConnection,
HTTPREQ_ReadFileEx,
HTTPREQ_WriteFile,
HTTPREQ_QueryDataAvailable,
- NULL
+ NULL,
+ HTTPREQ_LockRequestFile
};
/***********************************************************************
{
appinfo_t *hIC = session->appInfo;
http_request_t *request;
- DWORD len, res = ERROR_SUCCESS;
+ DWORD len;
TRACE("-->\n");
INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
sizeof(HINTERNET));
- TRACE("<-- %u (%p)\n", res, request);
-
- if(res != ERROR_SUCCESS) {
- WININET_Release( &request->hdr );
- *ret = NULL;
- return res;
- }
+ TRACE("<-- (%p)\n", request);
*ret = request->hdr.hInternet;
return ERROR_SUCCESS;
WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
BOOL custom_port = FALSE;
- static WCHAR httpW[] = {'h','t','t','p',0};
- static WCHAR httpsW[] = {'h','t','t','p','s',0};
+ static const WCHAR httpW[] = {'h','t','t','p',0};
+ static const WCHAR httpsW[] = {'h','t','t','p','s',0};
userName[0] = 0;
hostName[0] = 0;
netconn_t *netconn = NULL;
DWORD res;
- assert(!request->netconn);
reset_data_stream(request);
+ if (request->netconn)
+ {
+ if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
+ {
+ *reusing = TRUE;
+ return ERROR_SUCCESS;
+ }
+ else
+ {
+ free_netconn(request->netconn);
+ request->netconn = NULL;
+ }
+ }
+
res = HTTP_ResolveName(request);
if(res != ERROR_SUCCESS)
return res;
netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
list_remove(&netconn->pool_entry);
- if(NETCON_is_alive(netconn))
+ if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
break;
TRACE("connection %p closed during idle\n", netconn);
char *ascii_req;
loop_next = FALSE;
- reusing_connection = request->netconn != NULL;
if(redirected) {
request->contentLength = ~0u;
TRACE("Request header -> %s\n", debugstr_w(requestString) );
- if (!reusing_connection && (res = open_http_connection(request, &reusing_connection)) != ERROR_SUCCESS)
+ res = open_http_connection(request, &reusing_connection);
+ if (res != ERROR_SUCCESS)
break;
/* send the request as ASCII, tack on the optional data */
INT responseLen;
DWORD res = ERROR_SUCCESS;
- if(!request->netconn) {
+ if(!is_valid_netconn(request->netconn)) {
WARN("Not connected\n");
send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
return ERROR_INTERNET_OPERATION_CANCELLED;
TRACE("-->\n");
- if(!request->netconn)
+ if(!is_valid_netconn(request->netconn))
goto lend;
/* clear old response headers (eg. from a redirect response) */
# include <netinet/tcp.h>
#endif
+#include <errno.h>
+
#define RESPONSE_TIMEOUT 30 /* FROM internet.c */
+#ifdef MSG_DONTWAIT
+#define WINE_MSG_DONTWAIT MSG_DONTWAIT
+#else
+#define WINE_MSG_DONTWAIT 0
+#endif
+
/* FIXME!!!!!!
* This should use winsock - To use winsock the functions will have to change a bit
* as they are designed for unix sockets.
}
}
if(result == -1)
+ {
closesocket(netconn->socket);
+ netconn->socket = -1;
+ }
else {
flag = 0;
ioctlsocket(netconn->socket, FIONBIO, &flag);
netconn->security_flags = security_flags | server->security_flags;
netconn->mask_errors = mask_errors;
list_init(&netconn->pool_entry);
+ SecInvalidateHandle(&netconn->ssl_ctx);
result = create_netconn_socket(server, netconn, timeout);
if (result != ERROR_SUCCESS) {
return result;
}
+BOOL is_valid_netconn(netconn_t *netconn)
+{
+ return netconn && netconn->socket != -1;
+}
+
+void close_netconn(netconn_t *netconn)
+{
+ closesocket(netconn->socket);
+ netconn->socket = -1;
+}
+
void free_netconn(netconn_t *netconn)
{
server_release(netconn->server);
heap_free(netconn->extra_buf);
netconn->extra_buf = NULL;
netconn->extra_len = 0;
- DeleteSecurityContext(&netconn->ssl_ctx);
+ if (SecIsValidHandle(&netconn->ssl_ctx))
+ DeleteSecurityContext(&netconn->ssl_ctx);
}
- closesocket(netconn->socket);
heap_free(netconn);
}
}
#endif
+static void set_socket_blocking(int socket, blocking_mode_t mode)
+{
+#if defined(__MINGW32__) || defined (_MSC_VER)
+ ULONG arg = mode == BLOCKING_DISALLOW;
+ ioctlsocket(socket, FIONBIO, &arg);
+#endif
+}
+
static DWORD netcon_secure_connect_setup(netconn_t *connection, BOOL compat_mode)
{
SecBuffer out_buf = {0, SECBUFFER_TOKEN, NULL}, in_bufs[2] = {{0, SECBUFFER_TOKEN}, {0, SECBUFFER_EMPTY}};
|ISC_REQ_SEQUENCE_DETECT|ISC_REQ_REPLAY_DETECT|ISC_REQ_MANUAL_CRED_VALIDATION;
if(!ensure_cred_handle())
- return FALSE;
+ return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
if(compat_mode) {
if(!have_compat_cred_handle)
TRACE("InitializeSecurityContext ret %08x\n", status);
if(status == SEC_E_OK) {
+ if(SecIsValidHandle(&connection->ssl_ctx))
+ DeleteSecurityContext(&connection->ssl_ctx);
+ connection->ssl_ctx = ctx;
+
if(in_bufs[1].BufferType == SECBUFFER_EXTRA)
FIXME("SECBUFFER_EXTRA not supported\n");
}
}
-
if(status != SEC_E_OK || res != ERROR_SUCCESS) {
- WARN("Failed to initialize security context failed: %08x\n", status);
+ WARN("Failed to establish SSL connection: %08x (%u)\n", status, res);
heap_free(connection->ssl_buf);
connection->ssl_buf = NULL;
- DeleteSecurityContext(&ctx);
return res ? res : ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
}
-
TRACE("established SSL connection\n");
- connection->ssl_ctx = ctx;
-
connection->secure = TRUE;
connection->security_flags |= SECURITY_FLAG_SECURE;
}
}
-static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, SIZE_T *ret_size, BOOL *eof)
+static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, blocking_mode_t mode, SIZE_T *ret_size, BOOL *eof)
{
const SIZE_T ssl_buf_size = conn->ssl_sizes.cbHeader+conn->ssl_sizes.cbMaximumMessage+conn->ssl_sizes.cbTrailer;
SecBuffer bufs[4];
SecBufferDesc buf_desc = {SECBUFFER_VERSION, sizeof(bufs)/sizeof(*bufs), bufs};
- SSIZE_T size, buf_len;
+ SSIZE_T size, buf_len = 0;
+ blocking_mode_t tmp_mode;
int i;
SECURITY_STATUS res;
assert(conn->extra_len < ssl_buf_size);
+ /* BLOCKING_WAITALL is handled by caller */
+ if(mode == BLOCKING_WAITALL)
+ mode = BLOCKING_ALLOW;
+
if(conn->extra_len) {
memcpy(conn->ssl_buf, conn->extra_buf, conn->extra_len);
buf_len = conn->extra_len;
conn->extra_len = 0;
heap_free(conn->extra_buf);
conn->extra_buf = NULL;
- }else {
- buf_len = recv(conn->socket, conn->ssl_buf+conn->extra_len, ssl_buf_size-conn->extra_len, 0);
- if(buf_len < 0) {
- WARN("recv failed\n");
- return FALSE;
- }
+ }
+ tmp_mode = buf_len ? BLOCKING_DISALLOW : mode;
+ set_socket_blocking(conn->socket, tmp_mode);
+ size = recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, tmp_mode == BLOCKING_ALLOW ? 0 : WINE_MSG_DONTWAIT);
+ if(size < 0) {
if(!buf_len) {
- *eof = TRUE;
- return TRUE;
+ if(errno == EAGAIN || errno == EWOULDBLOCK) {
+ TRACE("would block\n");
+ return WSAEWOULDBLOCK;
+ }
+ WARN("recv failed\n");
+ return ERROR_INTERNET_CONNECTION_ABORTED;
}
+ }else {
+ buf_len += size;
+ }
+
+ *ret_size = buf_len;
+
+ if(!buf_len) {
+ *eof = TRUE;
+ return ERROR_SUCCESS;
}
- *ret_size = 0;
*eof = FALSE;
do {
case SEC_I_CONTEXT_EXPIRED:
TRACE("context expired\n");
*eof = TRUE;
- return TRUE;
+ return ERROR_SUCCESS;
case SEC_E_INCOMPLETE_MESSAGE:
assert(buf_len < ssl_buf_size);
- size = recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, 0);
- if(size < 1)
- return FALSE;
+ set_socket_blocking(conn->socket, mode);
+ size = recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, mode == BLOCKING_ALLOW ? 0 : WINE_MSG_DONTWAIT);
+ if(size < 1) {
+ if(size < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ TRACE("would block\n");
+
+ /* FIXME: Optimize extra_buf usage. */
+ conn->extra_buf = heap_alloc(buf_len);
+ if(!conn->extra_buf)
+ return ERROR_NOT_ENOUGH_MEMORY;
+
+ conn->extra_len = buf_len;
+ memcpy(conn->extra_buf, conn->ssl_buf, conn->extra_len);
+ return WSAEWOULDBLOCK;
+ }
+
+ return ERROR_INTERNET_CONNECTION_ABORTED;
+ }
buf_len += size;
continue;
default:
WARN("failed: %08x\n", res);
- return FALSE;
+ return ERROR_INTERNET_CONNECTION_ABORTED;
}
} while(res != SEC_E_OK);
assert(!conn->peek_len);
conn->peek_msg_mem = conn->peek_msg = heap_alloc(bufs[i].cbBuffer - size);
if(!conn->peek_msg)
- return FALSE;
+ return ERROR_NOT_ENOUGH_MEMORY;
conn->peek_len = bufs[i].cbBuffer-size;
memcpy(conn->peek_msg, (char*)bufs[i].pvBuffer+size, conn->peek_len);
}
if(bufs[i].BufferType == SECBUFFER_EXTRA) {
conn->extra_buf = heap_alloc(bufs[i].cbBuffer);
if(!conn->extra_buf)
- return FALSE;
+ return ERROR_NOT_ENOUGH_MEMORY;
conn->extra_len = bufs[i].cbBuffer;
memcpy(conn->extra_buf, bufs[i].pvBuffer, conn->extra_len);
}
}
- return TRUE;
+ return ERROR_SUCCESS;
}
/******************************************************************************
* Basically calls 'recv()' unless we should use SSL
* number of chars received is put in *recvd
*/
-DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags, int *recvd)
+DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, blocking_mode_t mode, int *recvd)
{
*recvd = 0;
if (!len)
if (!connection->secure)
{
+ int flags = 0;
+
+ switch(mode) {
+ case BLOCKING_ALLOW:
+ break;
+ case BLOCKING_DISALLOW:
+ flags = WINE_MSG_DONTWAIT;
+ break;
+ case BLOCKING_WAITALL:
+ flags = MSG_WAITALL;
+ break;
+ }
+
+ set_socket_blocking(connection->socket, mode);
*recvd = recv(connection->socket, buf, len, flags);
return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
}
else
{
SIZE_T size = 0, cread;
- BOOL res, eof;
+ BOOL eof;
+ DWORD res;
if(connection->peek_msg) {
size = min(len, connection->peek_len);
connection->peek_msg_mem = connection->peek_msg = NULL;
}
/* check if we have enough data from the peek buffer */
- if(!(flags & MSG_WAITALL) || size == len) {
+ if(mode != BLOCKING_WAITALL || size == len) {
*recvd = size;
return ERROR_SUCCESS;
}
+
+ mode = BLOCKING_DISALLOW;
}
do {
- res = read_ssl_chunk(connection, (BYTE*)buf+size, len-size, &cread, &eof);
- if(!res) {
- WARN("read_ssl_chunk failed\n");
- if(!size)
- return ERROR_INTERNET_CONNECTION_ABORTED;
+ res = read_ssl_chunk(connection, (BYTE*)buf+size, len-size, mode, &cread, &eof);
+ if(res != ERROR_SUCCESS) {
+ if(res == WSAEWOULDBLOCK) {
+ if(size)
+ res = ERROR_SUCCESS;
+ }else {
+ WARN("read_ssl_chunk failed\n");
+ }
break;
}
}
size += cread;
- }while(!size || ((flags & MSG_WAITALL) && size < len));
+ }while(!size || (mode == BLOCKING_WAITALL && size < len));
TRACE("received %ld bytes\n", size);
*recvd = size;
- return ERROR_SUCCESS;
+ return res;
}
}
const CERT_CONTEXT *ret;
SECURITY_STATUS res;
- if (!connection->secure)
- return NULL;
-
res = QueryContextAttributesW(&connection->ssl_ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&ret);
return res == SEC_E_OK ? ret : NULL;
}