1/*
2 * Copyright 2010-2017 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Christophe Huriaux, c.huriaux@gmail.com
7 *		Adrien Destugues, pulkomandy@pulkomandy.tk
8 */
9
10
11#include <HttpResult.h>
12#include <Debug.h>
13
14
15using std::ostream;
16
17
18BHttpResult::BHttpResult(const BUrl& url)
19	:
20	fUrl(url),
21	fHeaders(),
22	fStatusCode(0)
23{
24}
25
26
27BHttpResult::BHttpResult(BMessage* archive)
28	:
29	BUrlResult(archive),
30	fUrl(archive->FindString("http:url")),
31	fHeaders(),
32	fStatusCode(archive->FindInt32("http:statusCode"))
33{
34	fStatusString = archive->FindString("http:statusString");
35
36	BMessage headers;
37	archive->FindMessage("http:headers", &headers);
38	fHeaders.PopulateFromArchive(&headers);
39}
40
41
42BHttpResult::BHttpResult(const BHttpResult& other)
43	:
44	fUrl(other.fUrl),
45	fHeaders(other.fHeaders),
46	fStatusCode(other.fStatusCode),
47	fStatusString(other.fStatusString)
48{
49}
50
51
52BHttpResult::~BHttpResult()
53{
54}
55
56
57// #pragma mark Result parameters modifications
58
59
60void
61BHttpResult::SetUrl(const BUrl& url)
62{
63	fUrl = url;
64}
65
66
67// #pragma mark Result parameters access
68
69
70const BUrl&
71BHttpResult::Url() const
72{
73	return fUrl;
74}
75
76
77BString
78BHttpResult::ContentType() const
79{
80	return Headers()["Content-Type"];
81}
82
83
84size_t
85BHttpResult::Length() const
86{
87	const char* length = Headers()["Content-Length"];
88	if (length == NULL)
89		return 0;
90	return atoi(length);
91}
92
93
94const BHttpHeaders&
95BHttpResult::Headers() const
96{
97	return fHeaders;
98}
99
100
101int32
102BHttpResult::StatusCode() const
103{
104	return fStatusCode;
105}
106
107
108const BString&
109BHttpResult::StatusText() const
110{
111	return fStatusString;
112}
113
114
115// #pragma mark Result tests
116
117
118bool
119BHttpResult::HasHeaders() const
120{
121	return fHeaders.CountHeaders() > 0;
122}
123
124
125// #pragma mark Overloaded members
126
127
128BHttpResult&
129BHttpResult::operator=(const BHttpResult& other)
130{
131	if (this == &other)
132		return *this;
133
134	fUrl = other.fUrl;
135	fHeaders = other.fHeaders;
136	fStatusCode = other.fStatusCode;
137	fStatusString = other.fStatusString;
138
139	return *this;
140}
141
142
143status_t
144BHttpResult::Archive(BMessage* target, bool deep) const
145{
146	status_t result = BUrlResult::Archive(target, deep);
147	if (result != B_OK)
148		return result;
149
150	target->AddString("http:url", fUrl);
151	target->AddInt32("http:statusCode", fStatusCode);
152	target->AddString("http:statusString", fStatusString);
153
154	BMessage headers;
155	fHeaders.Archive(&headers);
156	target->AddMessage("http:headers", &headers);
157
158	return B_OK;
159}
160
161
162/*static*/ BArchivable*
163BHttpResult::Instantiate(BMessage* archive)
164{
165	if (!validate_instantiation(archive, "BHttpResult"))
166		return NULL;
167
168	return new BHttpResult(archive);
169}
170