diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2012-08-12 12:27:31 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2012-08-12 12:27:31 +0200 |
commit | 5e46acf128f2a016bbc7dfb063ededa197795a7a (patch) | |
tree | b4a6ec15b4bba6fae66c9581e8252a1996f5758b | |
parent | 9678c28677997dbd34b35b04463af350a31b17ac (diff) | |
download | crawler-5e46acf128f2a016bbc7dfb063ededa197795a7a.tar.gz crawler-5e46acf128f2a016bbc7dfb063ededa197795a7a.tar.bz2 |
added a test for WinHttp, quite a nice web library native to Windows
-rw-r--r-- | tests/GNUmakefile | 2 | ||||
-rwxr-xr-x | tests/streamhtmlparser/Makefile.W32 | 17 | ||||
-rwxr-xr-x | tests/winhttp/Makefile.W32 | 33 | ||||
-rwxr-xr-x | tests/winhttp/test1.cpp | 101 |
4 files changed, 135 insertions, 18 deletions
diff --git a/tests/GNUmakefile b/tests/GNUmakefile index 09bc024..2e910c1 100644 --- a/tests/GNUmakefile +++ b/tests/GNUmakefile @@ -1,6 +1,6 @@ TOPDIR = .. -SUBDIRS = utils url streamhtmlparser libfetch curl psql sqlite typedetect +SUBDIRS = utils winhttp url streamhtmlparser libfetch curl psql sqlite typedetect -include $(TOPDIR)/makefiles/gmake/sub.mk diff --git a/tests/streamhtmlparser/Makefile.W32 b/tests/streamhtmlparser/Makefile.W32 index dcd2b5f..d7d268c 100755 --- a/tests/streamhtmlparser/Makefile.W32 +++ b/tests/streamhtmlparser/Makefile.W32 @@ -30,24 +30,7 @@ test2.exe: test2.obj local_all: local_clean: - @-erase -f *.RES *.DIFF *.ERR 2>NUL local_distclean: local_test: - -dummy: - @-for %%m in ( simple google ) do \ - @echo Using URL normalizer '%m'.. & \ - @exec_test test1 test1 "parse illegal protocol" %m parse www.andreasbaumann.cc & \ - @exec_test test1 test2 "parse normal start URL without slash" %m parse http://www.andreasbaumann.cc & \ - @exec_test test1 test3 "parse normal start URL with slash" %m parse http://www.andreasbaumann.cc/ & \ - @exec_test test1 test4 "parse normal URL" %m parse http://www.andreasbaumann.cc/index.html & \ - @exec_test test1 test5 "parse normal URL with default port" %m parse http://www.andreasbaumann.cc:80/index.html & \ - @exec_test test1 test6 "parse normal URL with non-standard port" %m parse http://www.andreasbaumann.cc:8080/index.html & \ - @exec_test test1 test100 "normalize a relative URL" %m normalize http://www.andreasbaumann.cc/index.html /software.html & \ - @exec_test test1 test101 "absolute URL in HTML content" %m normalize http://www.andreasbaumann.cc/index.html http://www.yahoo.com/page.html & \ - @exec_test test1 test102 "path normalization, relative path" %m normalize http://www.andreasbaumann.cc/adir/index.html bdir/page.html & \ - @exec_test test1 test103 "path normalization, absolute path" %m normalize http://www.andreasbaumann.cc/adir/index.html /bdir/page.html & \ - @exec_test test1 test104 "path normalization, current dir" %m normalize http://www.andreasbaumann.cc/adir/index.html ./bdir/page.html & \ - @exec_test test1 test105 "path normalization, previous dir" %m normalize http://www.andreasbaumann.cc/adir/index.html ../bdir/page.html diff --git a/tests/winhttp/Makefile.W32 b/tests/winhttp/Makefile.W32 new file mode 100755 index 0000000..a77b105 --- /dev/null +++ b/tests/winhttp/Makefile.W32 @@ -0,0 +1,33 @@ +TOPDIR = ..\.. + +SUBDIRS = + +!INCLUDE $(TOPDIR)\makefiles\nmake\platform.mk + +INCLUDE_CXXFLAGS = \ + /D_WIN32_WINNT=0x504 + +INCLUDE_DIRS = \ + /I. + +INCLUDE_LDFLAGS = \ + +INCLUDE_LIBS = \ + WinHttp.lib + +TEST_CPP_BINS = \ + test1.exe + +OBJS = + +!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk + +test1.exe: test1.obj + +local_all: + +local_clean: + +local_distclean: + +local_test: diff --git a/tests/winhttp/test1.cpp b/tests/winhttp/test1.cpp new file mode 100755 index 0000000..e565cac --- /dev/null +++ b/tests/winhttp/test1.cpp @@ -0,0 +1,101 @@ +#define WIN32_MEAN_AND_LEAN +#include <windows.h> +#include <WinHttp.h> + +#include <iostream> +#include <string> + +using namespace std; + +int main( void ) +{ + HINTERNET session = WinHttpOpen( L"WinHTTP CrawlingWolf/0.0.1", + WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, + WINHTTP_NO_PROXY_NAME, + WINHTTP_NO_PROXY_BYPASS, 0 ); + if( !session ) { + cerr << "WinHttpOpen failed: " << GetLastError( ) << endl; + return 1; + } + + HINTERNET connect = WinHttpConnect( session, L"www.andreasbaumann.cc", + INTERNET_DEFAULT_HTTP_PORT, 0 ); + if( !connect ) { + cerr << "WinHttpConnect failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( session ); + return 1; + } + + HINTERNET request = WinHttpOpenRequest( connect, L"GET", L"/index.shtml", + NULL, WINHTTP_NO_REFERER, NULL, NULL ); + if( !request ) { + cerr << "WinHttpOpenRequest failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + + if( !WinHttpSendRequest( request, WINHTTP_NO_ADDITIONAL_HEADERS, + 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 ) ) { + cerr << "WinHttpSendRequest failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + + if( !WinHttpReceiveResponse( request, NULL ) ) { + cerr << "WinHttpReceiveResponse failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + + DWORD size; + string content; + size_t pos = 0; + do { + size = 0; + if( !WinHttpQueryDataAvailable( request, &size ) ) { + cerr << "WinHttpQueryDataAvailable failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + + LPSTR buf = new char[size+1]; + if( !buf ) { + cerr << "Out of memory allocating receive buffer" << endl; + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + ZeroMemory( buf, size+1 ); + + DWORD received; + if( !WinHttpReadData( request, (LPVOID)buf, size, &received ) ) { + cerr << "WinHttpReadData failed: " << GetLastError( ) << endl; + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( request ); + WinHttpCloseHandle( session ); + return 1; + } + + content.insert( pos, buf, received ); + pos += received; + + delete buf; + } while( size > 0 ); + + cout << "Received bytes: " << content.size( ) << endl; + cout << content << endl; + + WinHttpCloseHandle( request ); + WinHttpCloseHandle( connect ); + WinHttpCloseHandle( session ); + + return 0; +} |