1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | #include "DataRequest.h" |
11 | |
12 | #include <HttpAuthentication.h> |
13 | #include <mail_encoding.h> |
14 | #include <stdio.h> |
15 | |
16 | |
17 | BDataRequest::BDataRequest(const BUrl& url, BUrlProtocolListener* listener, |
18 | BUrlContext* context) |
19 | : BUrlRequest(url, listener, context, "data URL parser", "data"), |
20 | fResult() |
21 | { |
22 | fResult.SetContentType("text/plain"); |
23 | } |
24 | |
25 | |
26 | const BUrlResult& |
27 | BDataRequest::Result() const |
28 | { |
29 | return fResult; |
30 | } |
31 | |
32 | |
33 | status_t |
34 | BDataRequest::_ProtocolLoop() |
35 | { |
36 | BString mimeType; |
37 | BString charset; |
38 | const char* payload; |
39 | size_t length; |
40 | bool isBase64 = false; |
41 | |
42 | fUrl.UrlDecode(true); |
43 | BString data = fUrl.Path(); |
44 | int separatorPosition = data.FindFirst(','); |
45 | |
46 | if (fListener != NULL__null) |
| |
47 | fListener->ConnectionOpened(this); |
48 | |
49 | if (separatorPosition >= 0) { |
| 2 | | Assuming 'separatorPosition' is >= 0 | |
|
| |
50 | BString meta = data; |
51 | meta.Truncate(separatorPosition); |
52 | data.Remove(0, separatorPosition + 1); |
53 | |
54 | int pos = 0; |
55 | while(meta.Length() > 0) |
| 4 | | Loop condition is true. Entering loop body | |
|
| 8 | | Loop condition is false. Execution continues on line 78 | |
|
56 | { |
57 | |
58 | pos = meta.FindFirst(';', pos); |
59 | |
60 | BString parameter = meta; |
61 | if(pos >= 0) { |
| |
| |
62 | parameter.Truncate(pos); |
63 | meta.Remove(0, pos+1); |
64 | } else |
65 | meta.Truncate(0); |
66 | |
67 | |
68 | if(parameter == "base64") { |
| |
69 | isBase64 = true; |
70 | } else if(parameter.FindFirst("charset=") == 0) { |
71 | charset = parameter; |
72 | } else { |
73 | |
74 | mimeType = parameter; |
75 | } |
76 | } |
77 | |
78 | if (charset.Length() > 0) |
| |
79 | mimeType << ";" << charset; |
80 | fResult.SetContentType(mimeType); |
81 | } |
82 | |
83 | if (isBase64) { |
| |
84 | char* buffer = new char[data.Length() * 4 / 3]; |
| |
85 | payload = buffer; |
86 | |
87 | |
88 | length = decode_base64(buffer, data.String(), data.Length()); |
89 | } else { |
90 | payload = data.String(); |
91 | length = data.Length(); |
92 | } |
93 | |
94 | fResult.SetLength(length); |
95 | |
96 | if (fListener != NULL__null) { |
| |
97 | fListener->DownloadProgress(this, length, length); |
98 | if (length > 0) |
99 | fListener->DataReceived(this, payload, length); |
100 | } |
101 | |
102 | if (isBase64) |
| |
103 | delete payload; |
| 14 | | Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete' |
|
104 | |
105 | return B_PROT_SUCCESS; |
106 | } |