Ensures normalization on waitfe/fc-xml operations
[anna.git] / source / http / Message.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/functions.hpp>
11
12 #include <anna/xml/Compiler.hpp>
13
14 #include <anna/http/Message.hpp>
15 #include <anna/http/internal/defines.hpp>
16 #include <anna/http/Header.hpp>
17
18 using namespace std;
19 using namespace anna;
20
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);
25
26   if(contentType == NULL)
27     contentType = createHeader(http::Header::Type::ContentType);
28
29   contentType->setValue("text/xml");
30   return this;
31 }
32
33 http::Header* http::Message::find(const http::Header::Type::_v type)
34 throw() {
35   http::Header* result = NULL;
36
37   for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++) {
38     if(header(ii)->getType() == type) {
39       result = header(ii);
40       break;
41     }
42   }
43
44   return result;
45 }
46
47 http::Header* http::Message::find(const char* name)
48 throw() {
49   Header* result = NULL;
50   Header* w;
51   const string* s;
52
53   for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++) {
54     w = header(ii);
55
56     if(w->getCategory() != Header::Category::Extension)
57       continue;
58
59     if((s = w->getExtensionName()) == NULL)
60       continue;
61
62     if(*s == name) {
63       result = w;
64       break;
65     }
66   }
67
68   return result;
69 }
70
71 //-----------------------------------------------------------------------------------------------
72 // Codifica el mensaje HTTP.
73 //
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();
81
82   if(contentLength != NULL) {
83     if(body.isEmpty() == false) {
84       if(contentLength->getIntegerValue() != body.getSize())
85         contentLength->setValue(body.getSize());
86     } else
87       a_headers.release(contentLength);
88   } else {
89     if(body.isEmpty() == false) {
90       contentLength = createHeader(Header::Type::ContentLength);
91       contentLength->setValue(body.getSize());
92     }
93   }
94
95   a_codeBuffer->clear();
96   codeLine(codeStartLine());
97
98   for(header_iterator ii = header_begin(), maxii = header_end(); ii != maxii; ii ++)
99     codeLine(Message::header(ii)->code());
100
101   a_codeBuffer->append(endOfLine, sizeEndOfLine);
102   a_codeBuffer->append(body);
103   LOGDEBUG(
104     string msg("anna:http::Message::code | Message: ");
105     msg += anna::functions::asString(*a_codeBuffer);
106     Logger::debug(msg, ANNA_FILE_LOCATION);
107   );
108   return *a_codeBuffer;
109 }
110
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);
115 }