1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/functions.hpp>
12 #include <anna/xml/Compiler.hpp>
14 #include <anna/http/Message.hpp>
15 #include <anna/http/internal/defines.hpp>
16 #include <anna/http/Header.hpp>
21 comm::Message* http::Message::setBody(const xml::Node* node)
22 throw(RuntimeException) {
23 comm::Message::setBody(node);
24 http::Header* contentType = find(http::Header::Type::ContentType);
26 if(contentType == NULL)
27 contentType = createHeader(http::Header::Type::ContentType);
29 contentType->setValue("text/xml");
33 http::Header* http::Message::find(const http::Header::Type::_v type)
35 http::Header* result = NULL;
37 for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++) {
38 if(header(ii)->getType() == type) {
47 http::Header* http::Message::find(const char* name)
49 Header* result = NULL;
53 for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++) {
56 if(w->getCategory() != Header::Category::Extension)
59 if((s = w->getExtensionName()) == NULL)
71 //-----------------------------------------------------------------------------------------------
72 // Codifica el mensaje HTTP.
74 // Lo unico complicado a tener en cuenta es que el valor del content-length coincida con el
75 // valor de la longitud real de 'body'.
76 //-----------------------------------------------------------------------------------------------
77 const DataBlock& http::Message::code()
78 throw(RuntimeException) {
79 Header* contentLength = const_cast <Header*>(find(Header::Type::ContentLength));
80 const DataBlock& body = getBody();
82 if(contentLength != NULL) {
83 if(body.isEmpty() == false) {
84 if(contentLength->getIntegerValue() != body.getSize())
85 contentLength->setValue(body.getSize());
87 a_headers.release(contentLength);
89 if(body.isEmpty() == false) {
90 contentLength = createHeader(Header::Type::ContentLength);
91 contentLength->setValue(body.getSize());
95 a_codeBuffer->clear();
96 codeLine(codeStartLine());
98 for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++)
99 codeLine(Message::header(ii)->code());
101 a_codeBuffer->append(endOfLine, sizeEndOfLine);
102 a_codeBuffer->append(body);
104 string msg("anna:http::Message::code | Message: ");
105 msg += anna::functions::asString(*a_codeBuffer);
106 Logger::debug(msg, ANNA_FILE_LOCATION);
108 return *a_codeBuffer;
111 void http::Message::codeLine(const std::string& line)
112 throw(RuntimeException) {
113 a_codeBuffer->append(line.c_str(), line.length());
114 a_codeBuffer->append(endOfLine, sizeEndOfLine);