vfs: check userland buffers before reading them.
[haiku.git] / src / kits / network / libnetapi / HttpResult.cpp
bloba9013b7064e47e2b5a6a65de037ee33eeb0b65b1
1 /*
2 * Copyright 2010-2017 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 * Adrien Destugues, pulkomandy@pulkomandy.tk
8 */
11 #include <HttpResult.h>
12 #include <Debug.h>
15 using std::ostream;
18 BHttpResult::BHttpResult(const BUrl& url)
20 fUrl(url),
21 fHeaders(),
22 fStatusCode(0)
27 BHttpResult::BHttpResult(BMessage* archive)
29 BUrlResult(archive),
30 fUrl(archive->FindString("http:url")),
31 fHeaders(),
32 fStatusCode(archive->FindInt32("http:statusCode"))
34 fStatusString = archive->FindString("http:statusString");
36 BMessage headers;
37 archive->FindMessage("http:headers", &headers);
38 fHeaders.PopulateFromArchive(&headers);
42 BHttpResult::BHttpResult(const BHttpResult& other)
44 fUrl(other.fUrl),
45 fHeaders(other.fHeaders),
46 fStatusCode(other.fStatusCode),
47 fStatusString(other.fStatusString)
52 BHttpResult::~BHttpResult()
57 // #pragma mark Result parameters modifications
60 void
61 BHttpResult::SetUrl(const BUrl& url)
63 fUrl = url;
67 // #pragma mark Result parameters access
70 const BUrl&
71 BHttpResult::Url() const
73 return fUrl;
77 BString
78 BHttpResult::ContentType() const
80 return Headers()["Content-Type"];
84 size_t
85 BHttpResult::Length() const
87 const char* length = Headers()["Content-Length"];
88 if (length == NULL)
89 return 0;
90 return atoi(length);
94 const BHttpHeaders&
95 BHttpResult::Headers() const
97 return fHeaders;
101 int32
102 BHttpResult::StatusCode() const
104 return fStatusCode;
108 const BString&
109 BHttpResult::StatusText() const
111 return fStatusString;
115 // #pragma mark Result tests
118 bool
119 BHttpResult::HasHeaders() const
121 return fHeaders.CountHeaders() > 0;
125 // #pragma mark Overloaded members
128 BHttpResult&
129 BHttpResult::operator=(const BHttpResult& other)
131 if (this == &other)
132 return *this;
134 fUrl = other.fUrl;
135 fHeaders = other.fHeaders;
136 fStatusCode = other.fStatusCode;
137 fStatusString = other.fStatusString;
139 return *this;
143 status_t
144 BHttpResult::Archive(BMessage* target, bool deep) const
146 status_t result = BUrlResult::Archive(target, deep);
147 if (result != B_OK)
148 return result;
150 target->AddString("http:url", fUrl);
151 target->AddInt32("http:statusCode", fStatusCode);
152 target->AddString("http:statusString", fStatusString);
154 BMessage headers;
155 fHeaders.Archive(&headers);
156 target->AddMessage("http:headers", &headers);
158 return B_OK;
162 /*static*/ BArchivable*
163 BHttpResult::Instantiate(BMessage* archive)
165 if (!validate_instantiation(archive, "BHttpResult"))
166 return NULL;
168 return new BHttpResult(archive);