OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / http / httpresponse.cpp
1 #include "httpresponse.h"
2
3 HttpResponse::HttpResponse()
4         : m_version("1.1")
5         , m_status(200)
6 {
7
8 }
9
10 void HttpResponse::clear()
11 {
12         m_status = 200;
13         m_attributes.clear();
14 }
15
16 QByteArray HttpResponse::version() const
17 {
18         return m_version;
19 }
20
21 void HttpResponse::set_version(QByteArray const &ver)
22 {
23         m_version = ver;
24 }
25
26 int HttpResponse::status() const
27 {
28         return m_status;
29 }
30
31 void HttpResponse::set_status(int code)
32 {
33         m_status = code;
34 }
35
36 int HttpResponse::index_of(QByteArray const &name) const
37 {
38         QByteArray name_ = name.toLower();
39         int j = m_attributes.size();
40         for(int i = 0; i < j; i++)
41                 if(name_ == m_attributes.at(i).first.toLower()) return i;
42         return -1;
43 }
44 bool HttpResponse::has_attribute(QByteArray const &name) const
45 {
46         QByteArray name_ = name.toLower();
47         for(pair_type const &pair : m_attributes)
48                 if(name_ == pair.first.toLower()) return true;
49         return false;
50 }
51
52 QByteArray HttpResponse::attribute(QByteArray const& name) const
53 {
54         QByteArray name_ = name.toLower();
55         for(pair_type const &pair : m_attributes)
56                 if(name_ == pair.first.toLower()) return pair.second;
57         return QByteArray();
58
59 }
60
61 void HttpResponse::set_attribute(QByteArray const &name, QByteArray const &value)
62 {
63         int i = index_of(name);
64         if(0 <= i) m_attributes.removeAt(i);
65         m_attributes.append(qMakePair(name, value));
66 }
67
68 void HttpResponse::set_attribute(QByteArray const &name, int value)
69 {
70         set_attribute(name, QByteArray::number(value));
71 }
72
73 int HttpResponse::content_length() const
74 {
75         int i = index_of("content-length");
76         if(0 <= i) return m_attributes.at(i).second.toInt();
77         return Error;
78 }
79
80 char const* HttpResponse::status_string() const
81 {
82         switch (m_status)
83         {
84         case 100: return "100 Continue";
85         case 101: return "101 Switching Protocols";
86
87         case 200: return "200 OK";
88         case 201: return "201 Created";
89         case 202: return "202 Accepted";
90         case 203: return "203 Non-Authoritative Information";
91         case 204: return "204 No Content";
92         case 205: return "205 Reset Content";
93         case 206: return "206 Partial Content";
94
95         case 300: return "300 Multiple Choices";
96         case 301: return "301 Moved Permanently";
97         case 302: return "302 Found";
98         case 303: return "303 See Other";
99         case 304: return "304 Not Modified";
100         case 305: return "305 Use Proxy";
101         case 306: return "306 Unused";
102         case 307: return "307 Temporary Redirect";
103
104         case 400: return "400 Bad Request";
105         case 401: return "401 Unauthorized";
106         case 402: return "402 Payment Required";
107         case 403: return "403 Forbidden";
108         case 404: return "404 Not Found";
109         case 405: return "405 Method Not Allowed";
110         case 406: return "406 Not Acceptable";
111         case 407: return "407 Proxy Authentication Required";
112         case 408: return "408 Request Timeout";
113         case 409: return "409 Conflict";
114         case 410: return "410 Gone";
115         case 411: return "411 Length Required";
116         case 412: return "412 Precondition Failed";
117         case 413: return "413 Request Entity Too Large";
118         case 414: return "414 Request-Uri Too Long";
119         case 415: return "415 Unsupported Media Type";
120         case 416: return "416 Requested Range Not Satisfiable";
121         case 417: return "417 Expectation Failed";
122
123         case 500: return "500 Internal Server Error";
124         case 501: return "501 Not Implemented";
125         case 502: return "502 Bad Gateway";
126         case 503: return "503 Service Unavailable";
127         case 504: return "504 Gateway Timeout";
128         case 505: return "505 Http Version Not Supported";
129         }
130         Q_ASSERT(false);
131         return "";
132 }
133
134 QByteArray HttpResponse::to_latin_1() const
135 {
136         QByteArray result;
137
138         result.append("HTTP/").append(m_version).append(" ");
139         result.append(status_string()).append("\r\n");
140
141         for(pair_type const &pair : m_attributes)
142                 result.append(pair.first).append(": ").append(pair.second).append("\r\n");
143         result.append("\r\n");
144
145         return result;
146 }
147
148
149
150
151