-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadfile.c
More file actions
53 lines (44 loc) · 1.13 KB
/
Copy pathreadfile.c
File metadata and controls
53 lines (44 loc) · 1.13 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
#include <stdio.h>
#include <string.h>
#include <websocket/websocket.h>
#include "localfs.h"
/* Windows/Visual Studio quirks */
#ifdef _WIN32
#pragma warning(disable:4996)
#endif
int
read_file(wsk_ctx_t *ctx, const char *path)
{
char native[FILENAME_MAX];
wsk_byte_t *buffer;
unsigned n;
int retcode;
retcode = -1; // failure
buffer = wsk_alloc_block(ctx, 4096);
if (!normalized_path_to_native(path, native))
{
n = sprintf((char*)buffer, "HTTP/1.0 400 Incorrect file path \"%s\"\x0d\x0a"
"Server: libwebsockets\x0d\x0a"
"\x0d\x0a", path);
if (wsk_sendall(ctx, buffer, n) > 0)
retcode = 0;
}
else
{
FILE *fp;
size_t n;
fp = fopen(native, "rb");
if (fp == NULL) {
fprintf(stderr, "Failed to open file \"%s\"", native);
return -1; }
while ((n = fread(buffer, 1, 4096, fp)) > 0) {
if (wsk_sendall(ctx, buffer, n) <= 0) {
fprintf(stderr, "Error trying to send file content to client");
break;
}
}
fclose(fp);
}
if (buffer) wsk_free_block(ctx, buffer);
return retcode;
}