Pharos
0.7.23
Modern Web Framework & Web App Hosting Service.
Toggle main menu visibility
Loading...
Searching...
No Matches
native_http_response.c
Go to the documentation of this file.
1
/**
2
* @file
3
* @brief Implements native HTTP response writing helpers.
4
*/
5
6
#include "
native_http_response.h
"
7
8
#include <stdarg.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
13
int
native_connection_printf
(
NativeConnection
*connection,
const
char
*format, ...) {
14
char
stack_buf[1024];
15
va_list args;
16
int
len
;
17
va_start(args, format);
18
len
= vsnprintf(stack_buf,
sizeof
(stack_buf), format, args);
19
va_end(args);
20
if
(
len
< 0) {
21
return
-1;
22
}
23
if
((
size_t
)
len
<
sizeof
(stack_buf)) {
24
return
native_connection_write
(connection, stack_buf, (
size_t
)
len
);
25
}
26
{
27
char
*heap_buf = (
char
*)malloc((
size_t
)
len
+ 1);
28
if
(heap_buf == NULL) {
29
return
-1;
30
}
31
va_start(args, format);
32
vsnprintf(heap_buf, (
size_t
)
len
+ 1, format, args);
33
va_end(args);
34
len
=
native_connection_write
(connection, heap_buf, (
size_t
)
len
);
35
free(heap_buf);
36
return
len
;
37
}
38
}
39
40
void
native_send_text_response
(
NativeConnection
*connection,
int
status,
const
char
*status_text,
const
char
*body) {
41
size_t
body_len = strlen(body);
42
native_connection_printf
(
43
connection,
44
"HTTP/1.1 %d %s\r\n"
45
"Content-Type: text/plain; charset=utf-8\r\n"
46
"Content-Length: %zu\r\n"
47
"Connection: close\r\n"
48
"\r\n%s"
,
49
status,
50
status_text,
51
body_len,
52
body
53
);
54
}
native_connection_write
int native_connection_write(NativeConnection *connection, const void *buffer, size_t len)
Writes bytes to a native connection.
Definition
native_connection.c:187
native_send_text_response
void native_send_text_response(NativeConnection *connection, int status, const char *status_text, const char *body)
Sends a plain-text HTTP response with explicit content length.
Definition
native_http_response.c:40
native_connection_printf
int native_connection_printf(NativeConnection *connection, const char *format,...)
Writes formatted bytes to a native connection.
Definition
native_http_response.c:13
native_http_response.h
Declares native HTTP response writing helpers.
NativeConnection
Accepted client connection and its resolved transport metadata.
Definition
native_connection.h:24
len
const char size_t len
Definition
yyjson.h:8364
src
native_runtime
c
native_http_response.c
Generated by
1.17.0