ffead.server.doc
HttpResponseParser.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * HttpResponseParser.cpp
18  *
19  * Created on: Nov 27, 2010
20  * Author: sumeet
21  */
22 
23 #include "HttpResponseParser.h"
24 
25 string* HttpResponseParser::headernames = NULL;
26 
27 HttpResponseParser::HttpResponseParser() {
28  logger = Logger::getLogger("HttpResponseParser");
29 }
30 
31 HttpResponseParser::~HttpResponseParser() {
32  // TODO Auto-generated destructor stub
33 }
34 
35 
36 
37 HttpResponseParser::HttpResponseParser(string vecstr,string path)
38 {
39  vector<string> vec;
40  StringUtil::split(vec, vecstr, ("\n"));
41  if(HttpResponseParser::headernames==NULL)
42  {
43  HttpResponseParser::headernames = new string();
44  ifstream ifs((path+"http-res-headers").c_str(),ifstream::in);
45  string tempios;
46  while(getline(ifs,tempios,'\n'))
47  {
48  HttpResponseParser::headernames->append(tempios);
49  }
50  logger << "Done reading header types" << endl;
51  }
52  if(vec.size()!=0)
53  {
54  this->content = "";
55  string conten;
56  bool contStarts = false;
57  for(unsigned int i=0;i<vec.size();i++)
58  {
59  //logger << endl << "Reading line " << vec.at(i) << endl << flush;
60  vector<string> temp,vemp,memp;
61 
62  if(vec.at(i)=="\r" || vec.at(i)==""|| vec.at(i)=="\r\n")
63  {
64  contStarts = true;
65  continue;
66  }
67  StringUtil::split(temp, vec.at(i), (": "));
68  if(temp.size()>1)
69  {
70  //logger << "line => " << vec.at(i) << endl;
71  StringUtil::replaceFirst(temp.at(1),"\r","");
72  if(HttpResponseParser::headernames->find(temp.at(0)+":")!=string::npos)
73  {
74  //logger << temp.at(0) << " = " << temp.at(1) << endl;
75  this->headers[temp.at(0)] = temp.at(1);
76  }
77  else
78  logger << ("Not a valid header" + temp.at(0)) << endl;
79  }
80  else
81  {
82 
83  string tem = vec.at(i);
84  if(!contStarts)
85  {
86  //logger << "line => " << vec.at(i) << endl;
87  vector<string> httpst;
88  StringUtil::split(httpst, tem, (" "));
89  if(httpst.at(0).find("HTTP")==string::npos)
90  {
91 
92  }
93  else
94  {
95  this->headers["Version"] = httpst.at(0);
96  this->headers["StatusCode"] = httpst.at(1);
97  this->headers["StatusMsg"] = httpst.at(2);
98  }
99  }
100  else
101  {
102  string temp;
103  if(vec.at(i).find("<?")!=string::npos && vec.at(i).find("?>")!=string::npos)
104  {
105  temp = vec.at(i).substr(vec.at(i).find("?>")+2);
106  conten.append(temp);
107  }
108  else
109  conten.append(vec.at(i));
110  if(i!=vec.size()-1)
111  conten.append("\n");
112  }
113  }
114  }
115  this->content = conten;
116  if(this->content!="")
117  {
118  //logger << this->content << flush;
119  }
120  }
121 }
122 
123 HttpResponseParser::HttpResponseParser(string vecstr)
124 {
125  vector<string> vec;
126  StringUtil::split(vec, vecstr, ("\n"));
127  if(vec.size()!=0)
128  {
129  this->content = "";
130  string conten;
131  bool contStarts = false;
132  for(unsigned int i=0;i<vec.size();i++)
133  {
134  //logger << endl << "Reading line " << vec.at(i) << endl << flush;
135  vector<string> temp,vemp,memp;
136  if((vec.at(i)=="\r" || vec.at(i)==""|| vec.at(i)=="\r\n") && !contStarts)
137  {
138  contStarts = true;
139  continue;
140  }
141  StringUtil::split(temp, vec.at(i), (": "));
142  if(temp.size()>1 && !contStarts)
143  {
144  //logger << temp.at(0) << " => " << temp.at(1) << endl;
145  StringUtil::replaceFirst(temp.at(1),"\r","");
146  this->headers[temp.at(0)] = temp.at(1);
147  }
148  else
149  {
150  string tem = vec.at(i);
151  if(!contStarts)
152  {
153  //logger << "line => " << vec.at(i) << endl;
154  StringUtil::replaceFirst(tem,"\r","");
155  vector<string> httpst;
156  StringUtil::split(httpst, tem, (" "));
157  if(httpst.at(0).find("HTTP")==string::npos)
158  {
159 
160  }
161  else
162  {
163  this->headers["Version"] = httpst.at(0);
164  this->headers["StatusCode"] = httpst.at(1);
165  this->headers["StatusMsg"] = httpst.at(2);
166  }
167  }
168  else
169  {
170  conten.append(vec.at(i));
171  if(i!=vec.size()-1)
172  conten.append("\n");
173  }
174  }
175  }
176  this->content = conten;
177  if(this->content!="")
178  {
179  //logger << this->content << flush;
180  }
181  }
182 }