ffead.server.doc
HttpResponse.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  * HttpResponse.cpp
18  *
19  * Created on: Aug 19, 2009
20  * Author: sumeet
21  */
22 
23 #include "HttpResponse.h"
24 
25 HttpResponse::HttpResponse() {
26  // TODO Auto-generated constructor stub
27  this->server = "FFEAD 1.1";
28 }
29 
30 HttpResponse::~HttpResponse() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 string HttpResponse::generateResponse()
35 {
36  string resp;
37  resp = (httpVersion + " " + statusCode + " " + statusMsg + "\r\n");
38  if(this->server!="")resp += "Server: " + this->server + "\r\n";
39  if(this->date!="")resp += "Date: " + this->date + "\r\n";
40  if(this->connection!="")resp += "Connection: " + this->connection + "\r\n";
41  if(this->location!="")resp += "Location: " + this->location + "\r\n";
42  if(this->accept_ranges!="")resp += "Accept-Ranges: " + this->accept_ranges + "\r\n";
43  if(this->content_type!="")resp += "Content-Type: " + this->content_type + "\r\n";
44  if(this->content_str!="")resp += "Content-Length: " + CastUtil::lexical_cast<string>((int)content_str.length()) + "\r\n";
45  if(this->last_modified!="")resp += "Last-Modified: " + this->last_modified + "\r\n";
46  for (int var = 0; var < (int)this->cookies.size(); var++)
47  {
48  resp += "Set-Cookie: " + this->cookies.at(var) + "\r\n";
49  }
50  resp += "\r\n";
51  resp += this->content_str;
52  return resp;
53 }
54 
55 string HttpResponse::generateHeadResponse()
56 {
57  string resp;
58  resp = (httpVersion + " " + statusCode + " " + statusMsg + "\r\n");
59  if(this->server!="")resp += "Server: " + this->server + "\r\n";
60  if(this->date!="")resp += "Date: " + this->date + "\r\n";
61  if(this->connection!="")resp += "Connection: " + this->connection + "\r\n";
62  if(this->location!="")resp += "Location: " + this->location + "\r\n";
63  if(this->accept_ranges!="")resp += "Accept-Ranges: " + this->accept_ranges + "\r\n";
64  if(this->content_type!="")resp += "Content-Type: " + this->content_type + "\r\n";
65  if(this->content_str!="")resp += "Content-Length: " + CastUtil::lexical_cast<string>((int)content_str.length()) + "\r\n";
66  if(this->last_modified!="")resp += "Last-Modified: " + this->last_modified + "\r\n";
67  for (int var = 0; var < (int)this->cookies.size(); var++)
68  {
69  resp += "Set-Cookie: " + this->cookies.at(var) + "\r\n";
70  }
71  resp += "\r\n";
72  return resp;
73 }
74 
75 string HttpResponse::generateOptionsResponse()
76 {
77  string resp;
78  resp = (httpVersion + " " + statusCode + " " + statusMsg + "\r\n");
79  if(this->server!="")resp += "Server: " + this->server + "\r\n";
80  if(this->date!="")resp += "Date: " + this->date + "\r\n";
81  if(this->connection!="")resp += "Connection: " + this->connection + "\r\n";
82  for (int var = 0; var < (int)this->cookies.size(); var++)
83  {
84  resp += "Set-Cookie: " + this->cookies.at(var) + "\r\n";
85  }
86  resp += "Allow: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE\r\n";
87  resp += "\r\n";
88  return resp;
89 }
90 
91 string HttpResponse::generateTraceResponse(HttpRequest* req)
92 {
93  string resp;
94  resp = (httpVersion + " " + statusCode + " " + statusMsg + "\r\n");
95  if(this->server!="")resp += "Server: " + this->server + "\r\n";
96  if(this->date!="")resp += "Date: " + this->date + "\r\n";
97  if(this->connection!="")resp += "Connection: " + this->connection + "\r\n";
98  if(this->location!="")resp += "Location: " + this->location + "\r\n";
99  if(this->accept_ranges!="")resp += "Accept-Ranges: " + this->accept_ranges + "\r\n";
100  if(this->content_type!="")resp += "Content-Type: " + this->content_type + "\r\n";
101  if(this->content_str!="")resp += "Content-Length: " + CastUtil::lexical_cast<string>((int)content_str.length()) + "\r\n";
102  if(this->last_modified!="")resp += "Last-Modified: " + this->last_modified + "\r\n";
103  for (int var = 0; var < (int)this->cookies.size(); var++)
104  {
105  resp += "Set-Cookie: " + this->cookies.at(var) + "\r\n";
106  }
107  resp += "\r\n";
108  if(req!=NULL)
109  {
110  if(req->getHost()!="")resp += "Host: " + req->getHost() + "\r\n";
111  if(req->getUser_agent()!="")resp += "User-Agent: " + req->getUser_agent() + "\r\n";
112  if(req->getAccept()!="")resp += "Accept: " + req->getAccept() + "\r\n";
113  if(req->getAccept_lang()!="")resp += "Accept-Language: " + req->getAccept_lang() + "\r\n";
114  if(req->getAccept_encod()!="")resp += "Accept-Encoding: " + req->getAccept_encod() + "\r\n";
115  if(req->getAccept_chars()!="")resp += "Accept-Charset: " + req->getAccept_chars() + "\r\n";
116  if(req->getKeep_alive()!="")resp += "Keep-Alive: " + req->getKeep_alive() + "\r\n";
117  if(req->getConnection()!="")resp += "Connection: " + req->getConnection() + "\r\n";
118  if(req->getCache_ctrl()!="")resp += "Cache-Control: " + req->getCache_ctrl() + "\r\n";
119  if(req->getContent_type()!="")resp += "Content-Type: " + req->getContent_type() + "\r\n";
120  if(req->getContent_len()!="")resp += "Content-Length: " + req->getContent_len() + "\r\n";
121  if(req->getReferer()!="")resp += "Referer: " + req->getReferer() + "\r\n";
122  if(req->getPragma()!="")resp += "Pragma: " + req->getPragma() + "\r\n";
123  }
124  return resp;
125 }