Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 134 lines (97 sloc) 2.46 KB
#ifndef com_sleepless_lib_net_httpheadrequest_h
#define com_sleepless_lib_net_httpheadrequest_h
// Copyright 1998-2002
// Sleepless Software Inc.
// All Rights Reserved
/*
XXX generify this and put it somewhere else.
This header defines a struct called HTTPHeadRequest
that performs HTTP HEAD requests under win32.
***** THIS CODE IS WINDOWS SPECIFIC. ******
Supports HTTP and HTTPS (secure) protocols.
The constructor actually performs the request.
There is a getHeader() call to retrieve response headers.
(( see code for details ))
*/
#ifdef WIN32
#include <wininet.h>
struct HTTPHeadRequest
{
HINTERNET h;
HINTERNET hc;
HINTERNET hr;
char buf[1000];
bool failed;
HTTPHeadRequest(const char *host, int port, const char *path, bool ssl, bool reload, bool cacheIt)
{
static const char *acceptTypes[] = { "*/*", 0 };
failed = false;
h = InternetOpen( "Sleepless Software Inc.", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);
if(h == NULL)
{
failed = true;
return;
}
hc = InternetConnect(h, host, port, "Sleepless Software Inc.", "Sleepless Software Inc.", INTERNET_SERVICE_HTTP, 0, 0);
if(hc == NULL)
{
InternetCloseHandle(h);
failed = true;
return;
}
hr = HttpOpenRequest( hc, "HEAD", path, "HTTP/1.0", NULL, acceptTypes, (reload ? INTERNET_FLAG_RELOAD : 0) | (cacheIt ? INTERNET_FLAG_DONT_CACHE : 0) | INTERNET_FLAG_RAW_DATA | (ssl ? INTERNET_FLAG_SECURE : 0) | INTERNET_FLAG_EXISTING_CONNECT, NULL);
if(hr == NULL)
{
InternetCloseHandle(hc);
InternetCloseHandle(h);
failed = true;
return;
}
if( ! HttpSendRequest( hr, NULL, 0, NULL, 0) )
{
InternetCloseHandle(hr);
InternetCloseHandle(hc);
InternetCloseHandle(h);
failed = true;
return;
}
unsigned long sz = sizeof(buf);
if(!HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buf, &sz, NULL))
{
InternetCloseHandle(hr);
InternetCloseHandle(hc);
InternetCloseHandle(h);
failed = true;
return;
}
if(strcmp(buf, "200") != 0)
{
InternetCloseHandle(hr);
InternetCloseHandle(hc);
InternetCloseHandle(h);
failed = true;
return;
}
}
~HTTPHeadRequest()
{
if(!failed)
{
InternetCloseHandle(hr);
InternetCloseHandle(hc);
InternetCloseHandle(h);
}
}
const char *getHeader(const char *hdr)
{
if(failed)
return NULL;
strncpy(buf, hdr, sizeof(buf));
unsigned long sz = sizeof(buf);
if(!HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buf, &sz, NULL))
return NULL;
return buf;
}
};
#endif
#endif