This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathrocktree_http.h
148 lines (124 loc) · 3.7 KB
/
rocktree_http.h
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
147
148
#ifdef EMSCRIPTEN
#include <emscripten/fetch.h>
struct x {
int i;
void (*thunk)(int i, int error, uint8_t *d, size_t l);
};
// if you want to retain the data you need to copy it
void downloadSucceeded(emscripten_fetch_t *fetch) {
auto xx = (x*)(fetch->userData);
xx->thunk(xx->i, 0, (uint8_t*)fetch->data, (size_t)fetch->numBytes);
delete xx;
emscripten_fetch_close(fetch);
}
void downloadFailed(emscripten_fetch_t *fetch) {
//fprintf(stderr, "Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
auto xx = (x*)(fetch->userData);
xx->thunk(xx->i, 1, NULL, 0);
delete xx;
emscripten_fetch_close(fetch);
}
void fetchData(const char* path, int i, void (*thunk)(int i, int error, uint8_t *d, size_t l)) {
const char* base_url = "https://kh.google.com/rt/earth/";
char* url = (char*)malloc(strlen(base_url) + strlen(path) + 1);
strcpy(url, base_url); strcat(url, path);
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
auto xx = new x();
xx->i = i;
xx->thunk = thunk;
attr.userData = (void*)xx;
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = downloadSucceeded;
attr.onerror = downloadFailed;
emscripten_fetch(&attr, url);
return;
}
#else
#define HTTP_IMPLEMENTATION
#include "http.h"
std::once_flag cache_init_once_flag;
static const auto cache_pfx = "cache/";
static const auto cache_pfx_len = strlen(cache_pfx); // without 0
void createDir(const char* path);
bool readFile(const char* file_path, unsigned char** data, size_t* len);
void writeFile(const char* file_path, unsigned char* data, size_t len);
void fetchData(const char* path, int i, void (*thunk)(int i, int error, uint8_t *d, size_t l)) {
std::call_once(cache_init_once_flag, [](){
createDir(cache_pfx);
createDir((std::string(cache_pfx) + "BulkMetadata").c_str());
createDir((std::string(cache_pfx) + "NodeData").c_str());
});
auto use_cache = path[0] != 'P'; // don't cache planetoid
char *cache_path = (char*)malloc(cache_pfx_len + strlen(path) + 1);
sprintf(cache_path, "%s%s", cache_pfx, path);
{
unsigned char* data; size_t len;
if (use_cache && readFile(cache_path, &data, &len)) {
thunk(i, 0, data, len);
free(data);
free(cache_path);
return;
}
}
const char* base_url = "http://kh.google.com/rt/earth/";
char* url = (char*)malloc(strlen(base_url) + strlen(path) + 1);
strcpy(url, base_url); strcat(url, path);
//printf("GET %s\n", url);
http_t* request = http_get(url, NULL);
free(url);
if (!request) {
thunk(i, 1, NULL, 0);
free(cache_path);
return;
}
http_status_t status;
do {
status = http_process(request);
SDL_Delay(1);
} while (status == HTTP_STATUS_PENDING);
if (status == HTTP_STATUS_FAILED) {
http_release(request);
thunk(i, 1, NULL, 0);
free(cache_path);
return;
}
auto data = (unsigned char*)malloc(request->response_size);
auto len = request->response_size;
memcpy(data, request->response_data, len);
http_release(request);
thunk(i, 0, data, len);
if (use_cache) {
writeFile(cache_path, data, len);
}
free(data);
free(cache_path);
}
void createDir(const char* path) {
#ifdef _WIN32
_mkdir(path);
#else
mkdir(path, (mode_t)0755);
#endif
}
bool readFile(const char* file_path, unsigned char** data, size_t* len) {
return false;
FILE* file = fopen(file_path, "rb");
if (!file) return false;
fseek(file, 0, SEEK_END);
*len = ftell(file);
*data = (unsigned char*)malloc(*len);
fseek(file, 0, SEEK_SET);
fread(*data, *len, 1, file);
fclose(file);
return true;
}
void writeFile(const char* file_path, unsigned char* data, size_t len) {
return;
FILE* file = fopen(file_path, "wb");
if (!file) return;
fwrite(data, len, 1, file);
fclose(file);
}
#endif