-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
259 lines (224 loc) · 6.67 KB
/
app.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <string>
#include <iostream>
#include <sys/time.h>
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>
#include <cgicc/HTTPRedirectHeader.h>
#include <cgicc/HTTPStatusHeader.h>
#include "UrlShortener.h"
// This encapsulates the different user scenarios the service is suppose to handle.
class UrlShortenerApp
{
private:
struct QueryStrings
{
std::string c_AddString = "add";
std::string c_ListString = "list";
std::string c_AddAndListString = "addlist";
std::string c_gotoString = "goto";
std::string c_dnsName = "ec2-52-53-217-102.us-west-1.compute.amazonaws.com";
std::string c_pathString = "/c";
std::string c_appName = "urlshortener";
std::string c_shortUrlPrefix = "go";
};
public:
UrlShortenerApp(std::string args)
{
this->args = args;
const cgicc::CgiEnvironment& env = cgi.getEnvironment();
}
bool AddNewUrl( std::string queryString, bool fPrint = false )
{
std::string longUrl;
if( !FindUrl(queryString, longUrl) )
{
return false;
}
try
{
UrlMapping mapping;
if( longUrl.find_first_of("://") != std::string::npos )
{
mapping = m_shortener.AddNewUrlMapping( longUrl );
}
else
{
std::string qualifiedStr = "http://" + longUrl;
mapping = m_shortener.AddNewUrlMapping( qualifiedStr );
}
if( fPrint )
PrintSingleMapping( mapping.GetShortUrl() );
return true;
}
catch( ... )
{
return false;
}
}
bool GotoLongUrlFromQueryString( std::string queryString )
{
std::string shortUrl;
if( !FindUrl(queryString, shortUrl) )
{
return false;
}
try
{
std::string longUrl = m_shortener.GetLongUrlMapping(shortUrl).GetLongUrl();
std::cout << cgicc::HTTPRedirectHeader(longUrl);
return true;
}
catch( URLNotFoundException& e )
{
std::cout << cgicc::HTTPStatusHeader(404, "URL was not found!") << std::endl;
std::cout << "URL was not found!" << std::endl;
return false;
}
catch( ... )
{
return false;
}
}
bool GotoLongUrlFromShortUrl( std::string& shortUrl )
{
try
{
std::string longUrl = m_shortener.GetLongUrlMapping(shortUrl).GetLongUrl();
std::cout << cgicc::HTTPRedirectHeader(longUrl);
return true;
}
catch( URLNotFoundException& e )
{
std::cout << cgicc::HTTPStatusHeader(404, "URL was not found!") << std::endl;
std::cout << "URL was not found!" << std::endl;
return false;
}
catch( ... )
{
return false;
}
}
void PrintMappings( std::string /*queryString*/, std::string premessage = "" )
{
std::cout << cgicc::HTTPHTMLHeader() << std::endl;
std::cout << cgicc::html() << std::endl;
std::cout << cgicc::head(cgicc::title("URL Shortener")) << std::endl;
std::cout << cgicc::body() << std::endl;
std::cout << premessage << std::endl;
std::vector<UrlMapping> allMappings = m_shortener.GetAllMappings();
if( allMappings.size() > 0 )
{
std::cout << cgicc::h2("URLs:") << std::endl;
std::cout << cgicc::table().set("border","1") << std::endl;
std::cout << cgicc::tr() << cgicc::th("Short URLs") << cgicc::th("Long URLs") <<
cgicc::tr() << std::endl;
for( UrlMapping& m : allMappings )
{
std::cout << cgicc::tr() <<
cgicc::td() << cgicc::a(m.GetShortUrl()).set("href", "/" + queryStrings.c_shortUrlPrefix + "/" + m.GetShortUrl()) << cgicc::td();
std::cout << cgicc::td() << cgicc::a(m.GetLongUrl()).set("href",m.GetLongUrl()) << cgicc::td();
std::cout << cgicc::tr() << std::endl;
}
std::cout << cgicc::table() << std::endl;
}
else
{
std::cout << cgicc::h2("No URLs present!") << std::endl;
}
std::cout << cgicc::h3("Enter new Url to shorten:") << std::endl;
std::cout<< "<form action=\"" + queryStrings.c_pathString + "/" + queryStrings.c_appName + "\" method=\"get\" >" <<
"<textarea name=\"addlist\" cols=\"100\" rows=\"1\" ></textarea><br/>" <<
"<input type=\"submit\" value=\"Shorten!\" /> </form>" << std::endl;
std::cout << cgicc::body() << std::endl;
std::cout << cgicc::html() << std::endl;
}
void Perform()
{
const cgicc::CgiEnvironment& env = cgi.getEnvironment();
std::string queryString;
if( args.length() > 0 )
{
queryString = args;
}
else
{
queryString = env.getQueryString();
}
if( queryString.find( queryStrings.c_AddAndListString ) != std::string::npos )
{
if( AddNewUrl( queryString ) )
PrintMappings( queryString, "URL added succesfully");
else
PrintMappings( queryString, "Invalid parameters given to the service!");
}
else if( queryString.find( queryStrings.c_AddString ) != std::string::npos )
{
if( !AddNewUrl( queryString, true ) )
{
std::cout << cgicc::HTTPHTMLHeader() << std::endl;
std::cout << "Couldn't add new url!" << std::endl;
}
}
else if( queryString == "" || queryString.find( queryStrings.c_ListString ) != std::string::npos )
{
PrintMappings( queryString );
}
else if( queryString.find( queryStrings.c_gotoString ) != std::string::npos )
{
GotoLongUrlFromQueryString( queryString );
}
else{
PrintMappings( queryString, "Invalid Action specified!");
}
}
private:
void PrintSingleMapping( std::string shortUrl )
{
std::cout << cgicc::HTTPHTMLHeader() << std::endl;
std::cout << "http://" << queryStrings.c_dnsName << "/" << queryStrings.c_shortUrlPrefix << "/" <<
shortUrl << std::endl;
}
bool FindUrl( std::string queryString, std::string& url )
{
std::size_t foundEq = queryString.find("=");
if( foundEq == std::string::npos )
{
return false;
}
std::size_t foundAmp = queryString.find("&");
if( foundAmp == std::string::npos )
{
url = queryString.substr(foundEq + 1, queryString.size() );
}
else
{
url = queryString.substr(foundEq + 1, foundAmp );
}
url = cgicc::form_urldecode(url);
return !url.empty();
}
UrlShortener m_shortener;
QueryStrings queryStrings;
cgicc::Cgicc cgi;
std::string args;
};
int main(int argc, char** args)
{
try{
if( argc > 1 )
{
// This is used for local debugging purposes.
UrlShortenerApp app(args[1]);
app.Perform();
return 0;
}
UrlShortenerApp app("");
app.Perform();
}
catch(std::exception& e)
{
exit(1);
}
return 0;
}