Fixed the crash/Keep Alive issue, I think
[theodwalha.git] / theodwalha / reply.cpp
blobb0f7eba09cd849f975dcfd7b396f25d8daa68d22
1 #include <iostream>
2 #include <ail/string.hpp>
3 #include <ail/zlib.hpp>
4 #include <theodwalha/reply.hpp>
6 http_reply::http_reply():
7 gzip(false),
8 keep_alive(false)
12 void http_reply::ok()
14 code = 200;
15 description = "OK";
18 void http_reply::not_found()
20 code = 404;
21 description = "Not Found";
24 void http_reply::forbidden()
26 code = 403;
27 description = "Forbidden";
30 bool http_reply::get_packet(std::string & packet)
33 HTTP/1.1 200 OK
34 Date: Tue, 18 Aug 2009 03:14:21 GMT
35 Server: Apache/2.2.11 (Debian) PHP/5.2.9-4 with Suhosin-Patch
36 X-Powered-By: PHP/5.2.9-4
37 Vary: Accept-Encoding
38 Content-Encoding: gzip
39 Content-Length: 730
40 Keep-Alive: timeout=15, max=100
41 Connection: Keep-Alive
42 Content-Type: text/html
45 std::string const delimiter = "\r\n";
47 packet = std::string();
49 switch(protocol)
51 case http_protocol::version_1_1:
52 packet += "HTTP/1.1";
53 break;
55 case http_protocol::version_1_0:
56 packet += "HTTP/1.0";
57 break;
59 default:
60 std::cout << "Invalid protocol version specific in the HTTP reply" << std::endl;
61 return false;
64 packet += " " + ail::number_to_string(code) + " " + description + delimiter;
65 if(gzip)
67 try
69 std::string compressed_content;
70 ail::compress_gzip(content, compressed_content);
71 content = compressed_content;
73 catch(ail::exception & exception)
75 std::cout << exception.get_message() << std::endl;
77 packet += "Content-Encoding: gzip" + delimiter;
80 packet += "Content-Length: " + ail::number_to_string(content.size()) + delimiter;
82 if(keep_alive)
84 packet += "Keep-Alive: timeout=" + ail::number_to_string(keep_alive_timeout) + ", max=" + ail::number_to_string(keep_alive_max) + delimiter;
85 packet += "Connection: Keep-Alive" + delimiter;
88 packet += "Content-Type: " + content_type;
90 packet += delimiter + delimiter;
92 packet += content;
94 return true;