-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (136 loc) · 3.96 KB
/
Copy pathmain.cpp
File metadata and controls
146 lines (136 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <dirent.h>
#include <libpiper/server.h>
// Internal Error
static void internal_error(int sock) {
piper_response response;
response.content_type = SERVER_INTERNAL_ERROR;
response.content_length = 0;
piper_server_respond(sock, &response);
}
// Callback
static int _starts_with(const char *pre, const char *str) {
return strncmp(pre, str, strlen(pre)) == 0;
}
static int _ends_with(const char *suf, const char *str) {
size_t suf_len = strlen(suf);
size_t str_len = strlen(str);
return str_len >= suf_len && !strcmp(str + str_len - suf_len, suf);
}
static int callback(piper_request *request, int sock, __attribute__((unused)) void *user_data) {
// Log
printf("Serving: %s\n", request->path);
// Resolve
char *full_path = NULL;
{
// Make Path
std::string path;
path += "./";
path += request->path;
// Get CWD
char *cwd = getcwd(NULL, 0);
// Resolve
full_path = realpath(path.c_str(), NULL);
// Check
if (cwd == NULL || full_path == NULL || !_starts_with(cwd, full_path)) {
full_path = NULL;
}
// Free CWD
free(cwd);
}
// Check
if (full_path == NULL) {
static piper_response response;
response.content_type = SERVER_NOT_FOUND_ERROR;
response.content_length = 0;
if (piper_server_respond(sock, &response) != 0) {
internal_error(sock);
}
goto free;
}
// Choose Behavior
int is_dir;
{
struct stat st;
is_dir = stat(full_path, &st) == 0 && S_ISDIR(st.st_mode);
}
if (is_dir) {
// Force Directory Paths To End With '/'
if (!_ends_with("/", request->path)) {
// Redirect
piper_server_respond_str(sock, REDIRECT, "%s/", request->path);
goto free;
}
// Directory Listing
std::string page = "## Directory Listing";
// List Files
DIR *dir = opendir(full_path);
if (dir) {
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
page += "\n=> ";
page += entry->d_name;
if (entry->d_type == DT_DIR) {
page += '/';
}
}
closedir(dir);
}
// Sned
if (piper_server_respond_str(sock, UTF8_GEMTEXT, "%s", page.c_str()) != 0) {
internal_error(sock);
}
} else {
// File
std::ifstream file(full_path, std::ios::in | std::ios::binary);
if (file && file.good()) {
std::ostringstream data;
data << file.rdbuf();
file.close();
// Get Content Type
uint8_t content_type;
if (_ends_with(".gmi", full_path)) {
content_type = UTF8_GEMTEXT;
} else if (_ends_with(".txt", full_path)) {
content_type = UTF8_TEXT;
} else {
content_type = RAW_FILE;
}
// Send
if (piper_server_respond_str(sock, content_type, "%s", data.str().c_str()) != 0) {
internal_error(sock);
}
} else {
// Unable To Read
static piper_response response;
response.content_type = SERVER_NOT_FOUND_ERROR;
response.content_length = 0;
if (piper_server_respond(sock, &response) != 0) {
internal_error(sock);
}
}
}
// Free
free:
free(full_path);
// Return
return 0;
}
// Main
int main() {
printf("Starting File Server...\n");
// Run
if (piper_server_run(60, 10, callback, NULL) != 0) {
fprintf(stderr, "Failed To Start Server\n");
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}