Monitoring  3.3.4
O2 Monitoring library
UriParser.h
1 /*
2 Copyright (c) 2013 Covenant Eyes
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this
5 software and associated documentation files (the "Software"), to deal in the Software
6 without restriction, including without limitation the rights to use, copy, modify,
7 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8 permit persons to whom the Software is furnished to do so, subject to the following
9 conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies
12 or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21 
22 #ifndef ALICEO2_HTTPPARSER_H
23 #define ALICEO2_HTTPPARSER_H
24 
25 #include <iostream>
26 #include <string>
27 #include <stdlib.h>
28 
29 namespace http
30 {
31 struct url {
32  std::string protocol, user, password, host, path, search, url;
33  int port;
34 };
35 
36 //--- Helper Functions -------------------------------------------------------------~
37 static inline std::string TailSlice(std::string& subject, std::string delimiter, bool keep_delim = false)
38 {
39  // Chops off the delimiter and everything that follows (destructively)
40  // returns everything after the delimiter
41  auto delimiter_location = subject.find(delimiter);
42  auto delimiter_length = delimiter.length();
43  std::string output = "";
44 
45  if (delimiter_location < std::string::npos) {
46  auto start = keep_delim ? delimiter_location : delimiter_location + delimiter_length;
47  auto end = subject.length() - start;
48  output = subject.substr(start, end);
49  subject = subject.substr(0, delimiter_location);
50  }
51  return output;
52 }
53 
54 static inline std::string HeadSlice(std::string& subject, std::string delimiter)
55 {
56  // Chops off the delimiter and everything that precedes (destructively)
57  // returns everthing before the delimeter
58  auto delimiter_location = subject.find(delimiter);
59  auto delimiter_length = delimiter.length();
60  std::string output = "";
61  if (delimiter_location < std::string::npos) {
62  output = subject.substr(0, delimiter_location);
63  subject = subject.substr(delimiter_location + delimiter_length, subject.length() - (delimiter_location + delimiter_length));
64  }
65  return output;
66 }
67 
68 //--- Extractors -------------------------------------------------------------------~
69 static inline int ExtractPort(std::string& hostport)
70 {
71  int port;
72  std::string portstring = TailSlice(hostport, ":");
73  try {
74  port = atoi(portstring.c_str());
75  } catch (std::exception& e) {
76  port = -1;
77  }
78  return port;
79 }
80 
81 static inline std::string ExtractPath(std::string& in) { return TailSlice(in, "/", true); }
82 static inline std::string ExtractProtocol(std::string& in) { return HeadSlice(in, "://"); }
83 static inline std::string ExtractSearch(std::string& in) { return TailSlice(in, "?"); }
84 static inline std::string ExtractPassword(std::string& userpass) { return TailSlice(userpass, ":"); }
85 static inline std::string ExtractUserpass(std::string& in) { return HeadSlice(in, "@"); }
86 
87 //--- Public Interface -------------------------------------------------------------~
88 static inline url ParseHttpUrl(std::string& in)
89 {
90  url ret;
91  ret.port = -1;
92  ret.url = in;
93  ret.protocol = ExtractProtocol(in);
94  ret.search = ExtractSearch(in);
95  ret.path = ExtractPath(in);
96  std::string userpass = ExtractUserpass(in);
97  ret.password = ExtractPassword(userpass);
98  ret.user = userpass;
99  ret.port = ExtractPort(in);
100  ret.host = in;
101 
102  return ret;
103 }
104 } // namespace http
105 #endif
Definition: UriParser.h:29
Definition: UriParser.h:31