-
Notifications
You must be signed in to change notification settings - Fork 2
/
HttpUtils.cpp
147 lines (133 loc) · 4.61 KB
/
HttpUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* File: HttpUtils.cpp
* Author: user
*
* Created on 12. Oktober 2015, 23:14
*/
#include "HttpUtils.h"
class HttpIOReader : public BUrlProtocolListener {
public:
HttpIOReader(BPositionIO* data, BHttpHeaders* responseHeaders, size_t limit)
: fData(data),
fLimit(limit),
fHeaders(responseHeaders),
fIsRedirect(false)
{
}
~HttpIOReader() { }
virtual void HeadersReceived(BUrlRequest* caller, const BUrlResult& result) {
const BHttpResult* httpResult = dynamic_cast<const BHttpResult*>(&result);
if (httpResult && fHeaders) {
*fHeaders = httpResult->Headers();
fIsRedirect = BHttpRequest::IsRedirectionStatusCode(httpResult->StatusCode());
}
}
virtual void DataReceived(BUrlRequest* caller, const char* data, off_t position, ssize_t size) {
if (fIsRedirect)
return;
if (fData == NULL) {
caller->Stop();
return;
}
fData->Write(data, size);
if (fLimit) {
if (fLimit < size)
caller->Stop();
else
fLimit -= size;
}
}
private:
BPositionIO* fData;
BHttpHeaders* fHeaders;
bool fIsRedirect;
size_t fLimit;
};
class HttpHeaderReader : public BUrlProtocolListener {
public:
HttpHeaderReader(char* buffer = NULL, ssize_t size = 0)
: BUrlProtocolListener(),
fBuffer(buffer),
fSize(size),
fPos(0)
{ }
virtual void HeadersReceived(BUrlRequest* caller) {
if (fBuffer == NULL && caller->IsRunning())
caller->Stop();
}
virtual void DataReceived(BUrlRequest* caller, const char* data, off_t position, ssize_t size) {
if (fSize == 0 || fBuffer == NULL) {
if (caller->IsRunning())
caller->Stop();
return;
}
ssize_t receive = fSize - fPos;
if (size < receive)
receive = size;
memcpy(fBuffer + fPos, data, receive);
fPos += receive;
if (fPos >= fSize && caller->IsRunning())
caller->Stop();
}
private:
char* fBuffer;
ssize_t fSize;
off_t fPos;
};
/**
* Helper to make Http request and return body
* @param url Url to request
* @param accept Requested content type
* @param contentType Out: Received content type
* @return BMallocIO* filled with retrieved content
*/
BMallocIO* HttpUtils::GetAll(BUrl url, BHttpHeaders* responseHeaders, bigtime_t timeOut, BString* contentType, size_t sizeLimit) {
BMallocIO* data = new BMallocIO();
HttpIOReader reader(data, responseHeaders, sizeLimit);
HttpRequest request(url, false, "HTTP", &reader);
if (contentType && !contentType->IsEmpty()) {
BHttpHeaders* requestHeaders = new BHttpHeaders();
requestHeaders->AddHeader("accept", contentType->String());
request.AdoptHeaders(requestHeaders);
}
request.SetAutoReferrer(true);
request.SetFollowLocation(true);
request.SetTimeout(timeOut);
thread_id threadId = request.Run();
status_t status = B_OK;
wait_for_thread(threadId, &status);
int32 statusCode = request.Result().StatusCode();
size_t bufferLen = data->BufferLength();
if (!(statusCode == 0 || request.IsSuccessStatusCode(statusCode)) || bufferLen == 0) {
delete data;
data = NULL;
} else {
if (contentType)
contentType->SetTo(request.Result().ContentType());
}
return data;
}
status_t HttpUtils::GetStreamHeader(BUrl url, BHttpHeaders* responseHeaders) {
status_t status;
HttpIOReader reader(NULL, responseHeaders, 0);
HttpRequest request(url, false, "HTTP", &reader);
request.SetMethod("GET");
request.SetAutoReferrer(true);
request.SetFollowLocation(true);
request.SetTimeout(10000);
request.SetDiscardData(true);
request.SetUserAgent("WinampMPEG/5.55, Ultravox/2.1");
BHttpHeaders* requestHeaders = new BHttpHeaders();
requestHeaders->AddHeader("Icy-MetaData", 1);
request.AdoptHeaders(requestHeaders);
thread_id threadId = request.Run();
wait_for_thread(threadId, &status);
if (status == B_OK) {
BHttpResult result = request.Result();
if (result.StatusCode() == 200 || result.StatusCode() == B_OK) {
*responseHeaders = result.Headers();
} else
status = B_ERROR;
}
return status;
}