summaryrefslogtreecommitdiff
path: root/tests/url/test1.cpp
blob: 3dd6df2d1cf041c2757c62a1c317bcbcb33390c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "URL.hpp"
#ifdef USE_MODULELOADER
#include "URLNormalizer.hpp"
#include "ModuleLoader.hpp"
#else
#include "SimpleURLNormalizer.hpp"
#include "GoogleURLNormalizer.hpp"
#endif

#include <vector>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main( int argc, char *argv[] )
{
	try {
		if( argc < 3 ) {
			cerr << "usage: test1 <method> <action> <baseUrl> [<relativeUrl>]\n" << endl;
			return 1;
		}
		
		char *method = argv[1];
		char *action = argv[2];
		char *baseUrlString = argv[3];
		char *partialUrlString = argv[4];

#ifdef USE_MODULELOADER
		vector<string> modules;
#ifndef _WIN32
		modules.push_back( "../../src/modules/urlnormalizer/simpleurl/mod_urlnormalizer_simple.so" );
		modules.push_back( "../../src/modules/urlnormalizer/googleurl/mod_urlnormalizer_googleurl.so" );
#else
		modules.push_back( "..\\..\\src\\modules\\urlnormalizer\\simpleurl\\mod_urlnormalizer_simple.dll" );
		modules.push_back( "..\\..\\src\\modules\\urlnormalizer\\googleurl\\mod_urlnormalizer_googleurl.dll" );
#endif
		ModuleLoader<URLNormalizer> urlNormalizers( modules );

		URLNormalizer *normalizer = urlNormalizers.create( method );
#else
		URLNormalizer *normalizer;
		if( strcmp( method, "simple_urlnormalizer" ) == 0 ) {
			normalizer = new SimpleURLNormalizer( );
		} else if( strcmp( method, "google_urlnormalizer" ) == 0 ) {
			normalizer = new GoogleURLNormalizer( );
		} else {
			cerr << "Unknown normalization method '" << method << "'" << endl;
			return 1;
		}
#endif
		
		URL url;
		
		if( strcmp( action, "parse" ) == 0 ) {
			url = normalizer->parseUrl( baseUrlString );
		} else if( strcmp( action, "normalize" ) == 0 ) {
			URL baseUrl = normalizer->parseUrl( baseUrlString );
			if( baseUrl == URL::Null ) {
				cerr << "Illegal base URL!" << endl;
				return 1;
			}
			url = normalizer->normalize( baseUrl, partialUrlString );
		} else {
			cerr << "Unknown action '" << action << "'" << endl;
			return 1;
		}

		if( url == URL::Null ) {
			cerr << "Illegal URL!" << endl;
			return 1;
		}
		
		cout 	<< "protocol: " << url.protocol( ) << endl
			<< "host: " << url.host( ) << endl
			<< "port: " << url.port( ) << endl
			<< "path: " << url.path( ) << endl
			<< "query: " << url.query( ) << endl
			<< "fragment: " << url.fragment( ) << endl;

		cout	<< "URL: " << url << endl;

#ifdef USE_MODULELOADER
		urlNormalizers.destroy( normalizer );
#else
		delete normalizer;
#endif

		return 0;
	} catch( exception &e ) {
		cerr << e.what( ) << endl;
		return 1;
	}
}