comparison Core/HttpServer/HttpOutput.cpp @ 330:78a8eaa5f30b

cookies
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Jan 2013 11:41:13 +0100
parents 64925c94825c
children 639272ef7615
comparison
equal deleted inserted replaced
329:f579d50fdf8f 330:78a8eaa5f30b
48 { 48 {
49 Send(&s[0], s.size()); 49 Send(&s[0], s.size());
50 } 50 }
51 } 51 }
52 52
53 void HttpOutput::PrepareOkHeader(Header& header,
54 const char* contentType,
55 bool hasContentLength,
56 uint64_t contentLength,
57 const char* contentFilename)
58 {
59 header.clear();
60
61 if (contentType && contentType[0] != '\0')
62 {
63 header.push_back(std::make_pair("Content-Type", std::string(contentType)));
64 }
65
66 if (hasContentLength)
67 {
68 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength)));
69 }
70
71 if (contentFilename && contentFilename[0] != '\0')
72 {
73 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\"";
74 header.push_back(std::make_pair("Content-Disposition", attachment));
75 }
76 }
77
53 void HttpOutput::SendOkHeader(const char* contentType, 78 void HttpOutput::SendOkHeader(const char* contentType,
54 bool hasContentLength, 79 bool hasContentLength,
55 uint64_t contentLength, 80 uint64_t contentLength,
56 const char* contentFilename) 81 const char* contentFilename)
57 { 82 {
58 std::string s = "HTTP/1.1 200 OK\r\n"; 83 Header header;
59 84 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename);
60 if (contentType && contentType[0] != '\0') 85 SendOkHeader(header);
61 {
62 s += "Content-Type: " + std::string(contentType) + "\r\n";
63 }
64
65 if (hasContentLength)
66 {
67 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n";
68 }
69
70 if (contentFilename && contentFilename[0] != '\0')
71 {
72 s += "Content-Disposition: attachment; filename=\"" + std::string(contentFilename) + "\"\r\n";
73 }
74
75 s += "\r\n";
76
77 Send(&s[0], s.size());
78 } 86 }
79 87
80 void HttpOutput::SendOkHeader(const HttpHandler::Arguments& header) 88 void HttpOutput::SendOkHeader(const Header& header)
81 { 89 {
82 std::string s = "HTTP/1.1 200 OK\r\n"; 90 std::string s = "HTTP/1.1 200 OK\r\n";
83 91
84 for (HttpHandler::Arguments::const_iterator 92 for (Header::const_iterator
85 it = header.begin(); it != header.end(); it++) 93 it = header.begin(); it != header.end(); it++)
86 { 94 {
87 s += it->first + ": " + it->second + "\r\n"; 95 s += it->first + ": " + it->second + "\r\n";
88 } 96 }
89 97
131 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); 139 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL);
132 SendString(buffer); 140 SendString(buffer);
133 } 141 }
134 142
135 143
144 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
145 const std::string& contentType,
146 const HttpHandler::Arguments& cookies)
147 {
148 Header header;
149 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL);
150
151 for (HttpHandler::Arguments::const_iterator it = cookies.begin();
152 it != cookies.end(); it++)
153 {
154 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second));
155 }
156
157 SendOkHeader(header);
158 SendString(buffer);
159 }
160
161
136 void HttpOutput::AnswerBufferWithContentType(const void* buffer, 162 void HttpOutput::AnswerBufferWithContentType(const void* buffer,
137 size_t size, 163 size_t size,
138 const std::string& contentType) 164 const std::string& contentType)
139 { 165 {
140 SendOkHeader(contentType.c_str(), true, size, NULL); 166 SendOkHeader(contentType.c_str(), true, size, NULL);