Skip to content

Commit 43efc85

Browse files
authored
Merge pull request #18 from sparrowapp-dev/SPRW-931-test-flow-internal-feedback-ticket
feat: implement http request image support for cloud agent [SPRW-931]
2 parents 1423fef + d0915f1 commit 43efc85

File tree

1 file changed

+113
-14
lines changed

1 file changed

+113
-14
lines changed

src/proxy/http/http.service.ts

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,85 @@ export class HttpService {
1414
return { buffer: bstr, mime };
1515
}
1616

17+
private getStatusText(statusCode: number): string {
18+
const statusMap: Record<number, string> = {
19+
// 1xx Informational
20+
100: 'Continue',
21+
101: 'Switching Protocols',
22+
102: 'Processing',
23+
103: 'Early Hints',
24+
25+
// 2xx Success
26+
200: 'OK',
27+
201: 'Created',
28+
202: 'Accepted',
29+
203: 'Non-Authoritative Information',
30+
204: 'No Content',
31+
205: 'Reset Content',
32+
206: 'Partial Content',
33+
207: 'Multi-Status',
34+
208: 'Already Reported',
35+
226: 'IM Used',
36+
37+
// 3xx Redirection
38+
300: 'Multiple Choices',
39+
301: 'Moved Permanently',
40+
302: 'Found',
41+
303: 'See Other',
42+
304: 'Not Modified',
43+
305: 'Use Proxy',
44+
307: 'Temporary Redirect',
45+
308: 'Permanent Redirect',
46+
47+
// 4xx Client Errors
48+
400: 'Bad Request',
49+
401: 'Unauthorized',
50+
402: 'Payment Required',
51+
403: 'Forbidden',
52+
404: 'Not Found',
53+
405: 'Method Not Allowed',
54+
406: 'Not Acceptable',
55+
407: 'Proxy Authentication Required',
56+
408: 'Request Timeout',
57+
409: 'Conflict',
58+
410: 'Gone',
59+
411: 'Length Required',
60+
412: 'Precondition Failed',
61+
413: 'Payload Too Large',
62+
414: 'URI Too Long',
63+
415: 'Unsupported Media Type',
64+
416: 'Range Not Satisfiable',
65+
417: 'Expectation Failed',
66+
418: `I\'m a teapot`,
67+
421: 'Misdirected Request',
68+
422: 'Unprocessable Entity',
69+
423: 'Locked',
70+
424: 'Failed Dependency',
71+
425: 'Too Early',
72+
426: 'Upgrade Required',
73+
428: 'Precondition Required',
74+
429: 'Too Many Requests',
75+
431: 'Request Header Fields Too Large',
76+
451: 'Unavailable For Legal Reasons',
77+
78+
// 5xx Server Errors
79+
500: 'Internal Server Error',
80+
501: 'Not Implemented',
81+
502: 'Bad Gateway',
82+
503: 'Service Unavailable',
83+
504: 'Gateway Timeout',
84+
505: 'HTTP Version Not Supported',
85+
506: 'Variant Also Negotiates',
86+
507: 'Insufficient Storage',
87+
508: 'Loop Detected',
88+
509: 'Bandwidth Limit Exceeded',
89+
510: 'Not Extended',
90+
511: 'Network Authentication Required'
91+
};
92+
93+
return statusMap[statusCode] || 'Unknown Status';
94+
}
95+
1796
async makeHttpRequest({
1897
url,
1998
method,
@@ -128,24 +207,44 @@ export class HttpService {
128207
method: config.method,
129208
headers: config.headers,
130209
data: config.data,
210+
responseType: 'arraybuffer',
131211
});
132-
const resp = {
133-
status:
134-
response.status + ' ' + (response.statusText || 'Unknown Status'),
135-
data: response.data,
212+
213+
let contentType = response.headers['content-type'];
214+
let responseData = "";
215+
if (contentType?.startsWith('image/')) {
216+
const base64 = Buffer.from(response.data).toString('base64');
217+
responseData = `data:${contentType};base64,${base64}`;
218+
} else {
219+
responseData = Buffer.from(response.data).toString('utf-8');
220+
}
221+
222+
return {
223+
status: response.status + ' ' + (response.statusText || this.getStatusText(response.status)),
224+
data: `${responseData}`,
136225
headers: response.headers,
137226
};
138-
return resp;
139227
} catch (axiosError: any) {
140-
return {
141-
status: axiosError.response?.status
142-
? axiosError.response?.status +
143-
' ' +
144-
(axiosError.response?.statusText || 'Unknown Status')
145-
: null,
146-
data: axiosError.response?.data || { message: axiosError.message },
147-
headers: axiosError.response?.headers,
148-
};
228+
try{
229+
const responseData = Buffer.from(axiosError.response?.data).toString('utf-8');
230+
return {
231+
status: axiosError.response?.status
232+
? axiosError.response?.status +
233+
' ' +
234+
(axiosError.response?.statusText || this.getStatusText(axiosError.response?.status))
235+
: null,
236+
data: responseData || { message: axiosError.message },
237+
headers: axiosError.response?.headers,
238+
};
239+
240+
}
241+
catch(e){
242+
return {
243+
status: null,
244+
data: { message: axiosError.message },
245+
headers: axiosError.response?.headers,
246+
};
247+
}
149248
}
150249
} catch (error: any) {
151250
console.error('HTTP Service Error:', error);

0 commit comments

Comments
 (0)