Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_spawn_temp_body.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Writes multipart request bodies to temporary files and removes them during cleanup.
4 */
5
7
8#include <stdlib.h>
9#include <string.h>
10
11#ifdef _WIN32
12#include <io.h>
13#define pharos_spawn_temp_body_write _write
14#define pharos_spawn_temp_body_close _close
15#define pharos_spawn_temp_body_unlink _unlink
16#define pharos_spawn_temp_body_strdup _strdup
17#else
18#include <unistd.h>
19#define pharos_spawn_temp_body_write write
20#define pharos_spawn_temp_body_close close
21#define pharos_spawn_temp_body_unlink unlink
22#define pharos_spawn_temp_body_strdup strdup
23#endif
24
25#if defined(_WIN32)
26int write_native_temp_body_file(const char *body, size_t body_len, char **temp_body_path_out) {
27 (void)body;
28 (void)body_len;
29 if (temp_body_path_out != NULL) {
30 *temp_body_path_out = NULL;
31 }
32 return 78;
33}
34#else
35int write_native_temp_body_file(const char *body, size_t body_len, char **temp_body_path_out) {
36 char temp_template[] = "/tmp/pharos-body-XXXXXX";
37 int temp_fd;
38 if (temp_body_path_out == NULL) {
39 return 69;
40 }
41 *temp_body_path_out = NULL;
42 temp_fd = mkstemp(temp_template);
43 if (temp_fd < 0) {
44 return 69;
45 }
46 if (body_len > 0 && pharos_spawn_temp_body_write(temp_fd, body != NULL ? body : "", body_len) < 0) {
48 pharos_spawn_temp_body_unlink(temp_template);
49 return 69;
50 }
52 *temp_body_path_out = pharos_spawn_temp_body_strdup(temp_template);
53 if (*temp_body_path_out == NULL) {
54 pharos_spawn_temp_body_unlink(temp_template);
55 return 69;
56 }
57 return 0;
58}
59#endif
60
61void cleanup_native_temp_body_file(char **temp_body_path_io) {
62 if (temp_body_path_io == NULL || *temp_body_path_io == NULL) {
63 return;
64 }
65 pharos_spawn_temp_body_unlink(*temp_body_path_io);
66 free(*temp_body_path_io);
67 *temp_body_path_io = NULL;
68}
#define pharos_spawn_temp_body_strdup
int write_native_temp_body_file(const char *body, size_t body_len, char **temp_body_path_out)
Writes a request body to a temporary file for multipart request execution.
#define pharos_spawn_temp_body_write
#define pharos_spawn_temp_body_unlink
void cleanup_native_temp_body_file(char **temp_body_path_io)
Removes a temporary request-body file and clears its stored path.
#define pharos_spawn_temp_body_close
Declares temporary request-body file helpers.