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 <UrlProtocolDispatchingListener.h>
12
13 #include <Debug.h>
14 #include <UrlResult.h>
15
16 #include <assert.h>
17
18 using namespace BPrivate::Network;
19
20
21 const char* kUrlProtocolMessageType = "be:urlProtocolMessageType";
22 const char* kUrlProtocolCaller = "be:urlProtocolCaller";
23
24
BUrlProtocolDispatchingListener(BHandler * handler)25 BUrlProtocolDispatchingListener::BUrlProtocolDispatchingListener
26 (BHandler* handler)
27 :
28 fMessenger(handler)
29 {
30 }
31
32
BUrlProtocolDispatchingListener(const BMessenger & messenger)33 BUrlProtocolDispatchingListener::BUrlProtocolDispatchingListener
34 (const BMessenger& messenger)
35 :
36 fMessenger(messenger)
37 {
38 }
39
40
~BUrlProtocolDispatchingListener()41 BUrlProtocolDispatchingListener::~BUrlProtocolDispatchingListener()
42 {
43 }
44
45
46 void
ConnectionOpened(BUrlRequest * caller)47 BUrlProtocolDispatchingListener::ConnectionOpened(BUrlRequest* caller)
48 {
49 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
50 _SendMessage(&message, B_URL_PROTOCOL_CONNECTION_OPENED, caller);
51 }
52
53
54 void
HostnameResolved(BUrlRequest * caller,const char * ip)55 BUrlProtocolDispatchingListener::HostnameResolved(BUrlRequest* caller,
56 const char* ip)
57 {
58 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
59 message.AddString("url:hostIp", ip);
60
61 _SendMessage(&message, B_URL_PROTOCOL_HOSTNAME_RESOLVED, caller);
62 }
63
64
65 void
ResponseStarted(BUrlRequest * caller)66 BUrlProtocolDispatchingListener::ResponseStarted(BUrlRequest* caller)
67 {
68 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
69 _SendMessage(&message, B_URL_PROTOCOL_RESPONSE_STARTED, caller);
70 }
71
72
73 void
HeadersReceived(BUrlRequest * caller)74 BUrlProtocolDispatchingListener::HeadersReceived(BUrlRequest* caller)
75 {
76 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
77 _SendMessage(&message, B_URL_PROTOCOL_HEADERS_RECEIVED, caller);
78 }
79
80
81 void
BytesWritten(BUrlRequest * caller,size_t bytesWritten)82 BUrlProtocolDispatchingListener::BytesWritten(BUrlRequest* caller,
83 size_t bytesWritten)
84 {
85 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
86 message.AddInt32("url:bytesWritten", bytesWritten);
87
88 _SendMessage(&message, B_URL_PROTOCOL_BYTES_WRITTEN, caller);
89 }
90
91
92 void
DownloadProgress(BUrlRequest * caller,off_t bytesReceived,off_t bytesTotal)93 BUrlProtocolDispatchingListener::DownloadProgress(BUrlRequest* caller,
94 off_t bytesReceived, off_t bytesTotal)
95 {
96 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
97 message.AddInt64("url:bytesReceived", bytesReceived);
98 message.AddInt64("url:bytesTotal", bytesTotal);
99
100 _SendMessage(&message, B_URL_PROTOCOL_DOWNLOAD_PROGRESS, caller);
101 }
102
103
104 void
UploadProgress(BUrlRequest * caller,off_t bytesSent,off_t bytesTotal)105 BUrlProtocolDispatchingListener::UploadProgress(BUrlRequest* caller,
106 off_t bytesSent, off_t bytesTotal)
107 {
108 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
109 message.AddInt64("url:bytesSent", bytesSent);
110 message.AddInt64("url:bytesTotal", bytesTotal);
111
112 _SendMessage(&message, B_URL_PROTOCOL_UPLOAD_PROGRESS, caller);
113 }
114
115
116 void
RequestCompleted(BUrlRequest * caller,bool success)117 BUrlProtocolDispatchingListener::RequestCompleted(BUrlRequest* caller,
118 bool success)
119 {
120 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
121 message.AddBool("url:success", success);
122
123 _SendMessage(&message, B_URL_PROTOCOL_REQUEST_COMPLETED, caller);
124 }
125
126
127 void
DebugMessage(BUrlRequest * caller,BUrlProtocolDebugMessage type,const char * text)128 BUrlProtocolDispatchingListener::DebugMessage(BUrlRequest* caller,
129 BUrlProtocolDebugMessage type, const char* text)
130 {
131 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
132 message.AddInt32("url:type", type);
133 message.AddString("url:text", text);
134
135 _SendMessage(&message, B_URL_PROTOCOL_DEBUG_MESSAGE, caller);
136 }
137
138
139 bool
CertificateVerificationFailed(BUrlRequest * caller,BCertificate & certificate,const char * error)140 BUrlProtocolDispatchingListener::CertificateVerificationFailed(
141 BUrlRequest* caller, BCertificate& certificate, const char* error)
142 {
143 BMessage message(B_URL_PROTOCOL_NOTIFICATION);
144 message.AddString("url:error", error);
145 message.AddPointer("url:certificate", &certificate);
146 message.AddInt8(kUrlProtocolMessageType,
147 B_URL_PROTOCOL_CERTIFICATE_VERIFICATION_FAILED);
148 message.AddPointer(kUrlProtocolCaller, caller);
149
150 // Warning: synchronous reply
151 BMessage reply;
152 fMessenger.SendMessage(&message, &reply);
153
154 return reply.FindBool("url:continue");
155 }
156
157
158 void
_SendMessage(BMessage * message,int8 notification,BUrlRequest * caller)159 BUrlProtocolDispatchingListener::_SendMessage(BMessage* message,
160 int8 notification, BUrlRequest* caller)
161 {
162 ASSERT(message != NULL);
163
164 message->AddPointer(kUrlProtocolCaller, caller);
165 message->AddInt8(kUrlProtocolMessageType, notification);
166
167 #ifdef DEBUG
168 status_t result = fMessenger.SendMessage(message);
169 ASSERT(result == B_OK);
170 #else
171 fMessenger.SendMessage(message);
172 #endif
173 }
174