| 1 |
#include <stdint.h> |
| 2 |
#include <ctype.h> |
| 3 |
#include <ap_config.h> |
| 4 |
#include <http_core.h> |
| 5 |
#include <http_log.h> |
| 6 |
#include <http_protocol.h> |
| 7 |
#include <http_request.h> |
| 8 |
#include <apr_version.h> |
| 9 |
#include <apr_pools.h> |
| 10 |
#include <apr_strings.h> |
| 11 |
#include "unixd.h" |
| 12 |
|
| 13 |
#if APR_HAS_SHARED_MEMORY |
| 14 |
#include "apr_rmm.h" |
| 15 |
#include "apr_shm.h" |
| 16 |
#else |
| 17 |
#error "APR_HAS_SHARED_MEMORY required for upload_progress module" |
| 18 |
#endif |
| 19 |
|
| 20 |
#if APR_HAVE_UNISTD_H |
| 21 |
#include <unistd.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "ap_backports.h" |
| 25 |
|
| 26 |
#ifndef PROGRESS_ID |
| 27 |
# define PROGRESS_ID "X-Progress-ID" |
| 28 |
#endif |
| 29 |
#ifndef JSON_CB_PARAM |
| 30 |
# define JSON_CB_PARAM "callback" |
| 31 |
#endif |
| 32 |
#ifndef CACHE_FILENAME |
| 33 |
# define CACHE_FILENAME "/tmp/upload_progress_cache" |
| 34 |
#endif |
| 35 |
#ifndef PROGRESS_EXPIRES |
| 36 |
/* shared memory entries not updated in PROGRESS_EXPIRES seconds |
| 37 |
will be reused when needed */ |
| 38 |
# define PROGRESS_EXPIRES 60 |
| 39 |
#endif |
| 40 |
#ifndef UP_DEBUG |
| 41 |
# define UP_DEBUG 0 |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef ARG_ALLOWED_PROGRESSID |
| 45 |
# define ARG_ALLOWED_PROGRESSID "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_:./!{}" |
| 46 |
#endif |
| 47 |
#ifndef ARG_ALLOWED_JSONPCALLBACK |
| 48 |
# define ARG_ALLOWED_JSONPCALLBACK "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._$" |
| 49 |
#endif |
| 50 |
#ifndef ARG_MINLEN_PROGRESSID |
| 51 |
# define ARG_MINLEN_PROGRESSID 8 |
| 52 |
#endif |
| 53 |
#ifndef ARG_MAXLEN_PROGRESSID |
| 54 |
# define ARG_MAXLEN_PROGRESSID 128 |
| 55 |
#endif |
| 56 |
#define UP_STRINGIFY(arg) #arg |
| 57 |
#define UP_TOSTRING(arg) UP_STRINGIFY(arg) |
| 58 |
#define UP_ID_FMT "." UP_TOSTRING(ARG_MAXLEN_PROGRESSID) "s" |
| 59 |
#ifndef ARG_MINLEN_JSONPCALLBACK |
| 60 |
# define ARG_MINLEN_JSONPCALLBACK 1 |
| 61 |
#endif |
| 62 |
#ifndef ARG_MAXLEN_JSONPCALLBACK |
| 63 |
/* This limit is set by most JS implementations on identifier length */ |
| 64 |
# define ARG_MAXLEN_JSONPCALLBACK 64 |
| 65 |
#endif |
| 66 |
|
| •67 |
#ifdef APLOG_USE_MODULE |
| 68 |
APLOG_USE_MODULE(upload_progress); |
| 69 |
#endif |
| 70 |
|
| 71 |
#if UP_DEBUG > 0 |
| •72 |
# if !defined(APLOG_USE_MODULE) && UP_DEBUG > 1 |
| 73 |
# define up_log(m,s,err,srv,fmtstr,...) ap_log_error( m, s, err, srv, "pid:%" APR_PID_T_FMT " " fmtstr, getpid(), ##__VA_ARGS__ ) |
| 74 |
# else |
| 75 |
# define up_log(...) ap_log_error( __VA_ARGS__ ) |
| 76 |
# endif |
| 77 |
#else |
| 78 |
# define up_log(...) |
| 79 |
#endif |
| 80 |
|
| 81 |
#define CACHE_LOCK() do { \ |
| 82 |
if (config->cache_lock) { \ |
| 83 |
char errbuf[200]; \ |
| 84 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "CACHE_LOCK()"); \ |
| 85 |
apr_status_t status = apr_global_mutex_lock(config->cache_lock); \ |
| 86 |
if (status != APR_SUCCESS) { \ |
| 87 |
ap_log_error(APLOG_MARK, APLOG_CRIT, status, server, \ |
| 88 |
"%s", apr_strerror(status, errbuf, sizeof(errbuf))); \ |
| 89 |
} \ |
| 90 |
} \ |
| 91 |
} while (0) |
| 92 |
|
| 93 |
#define CACHE_UNLOCK() do { \ |
| 94 |
if (config->cache_lock) \ |
| 95 |
{ \ |
| 96 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "CACHE_UNLOCK()"); \ |
| 97 |
apr_global_mutex_unlock(config->cache_lock); \ |
| 98 |
} \ |
| 99 |
} while (0) |
| 100 |
|
| 101 |
typedef struct { |
| 102 |
int track_enabled; |
| 103 |
int report_enabled; |
| 104 |
} DirConfig; |
| 105 |
|
| 106 |
typedef struct upload_progress_node_s{ |
| 107 |
apr_off_t length; |
| 108 |
apr_off_t received; |
| 109 |
int err_status; |
| 110 |
time_t started_at; |
| 111 |
apr_off_t speed; /* bytes per second */ |
| 112 |
time_t updated_at; |
| 113 |
int done; |
| 114 |
char key[ARG_MAXLEN_PROGRESSID]; |
| 115 |
} upload_progress_node_t; |
| 116 |
|
| 117 |
typedef struct { |
| 118 |
apr_time_t cache_updated_at; |
| 119 |
upload_progress_node_t node; |
| 120 |
} upload_progress_req_t; |
| 121 |
|
| 122 |
typedef struct { |
| 123 |
int count; |
| 124 |
int active; |
| 125 |
} upload_progress_cache_t; |
| 126 |
|
| 127 |
typedef struct { |
| 128 |
apr_global_mutex_t *cache_lock; |
| 129 |
char *lock_file; /* filename for shm lock mutex */ |
| 130 |
apr_size_t cache_bytes; |
| 131 |
apr_shm_t *cache_shm; |
| 132 |
char *cache_file; |
| 133 |
upload_progress_cache_t *cache; |
| 134 |
int *list; /* static array of node indexes, list begins with indexes of active nodes */ |
| 135 |
upload_progress_node_t *nodes; /* all nodes allocated at once */ |
| 136 |
} ServerConfig; |
| 137 |
|
| 138 |
static void upload_progress_cache_write(server_rec *, upload_progress_node_t *); |
| 139 |
static upload_progress_node_t *find_node(server_rec *, const char *); |
| 140 |
static apr_status_t upload_progress_cleanup(void *); |
| 141 |
static const char *get_progress_id(request_rec *, int *); |
| 142 |
|
| 143 |
extern module AP_MODULE_DECLARE_DATA upload_progress_module; |
| 144 |
|
| 145 |
static server_rec *global_server = NULL; |
| 146 |
|
| 147 |
static inline ServerConfig *get_server_config(server_rec *s) |
| 148 |
{ |
| 149 |
return (ServerConfig *)ap_get_module_config(s->module_config, &upload_progress_module); |
| 150 |
} |
| 151 |
|
| 152 |
static inline DirConfig *get_dir_config(request_rec *r) |
| 153 |
{ |
| 154 |
return (DirConfig *)ap_get_module_config(r->per_dir_config, &upload_progress_module); |
| 155 |
} |
| 156 |
|
| 157 |
static inline upload_progress_req_t *get_request_config(request_rec *r) |
| 158 |
{ |
| 159 |
return (upload_progress_req_t *)ap_get_module_config(r->request_config, &upload_progress_module); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
static int upload_progress_handle_request(request_rec *r) |
| 164 |
{ |
| 165 |
server_rec *server = r->server; |
| 166 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "upload_progress_handle_request()"); |
| 167 |
|
| 168 |
DirConfig *dir = get_dir_config(r); |
| 169 |
|
| 170 |
if (!dir || (dir->track_enabled <= 0)) { |
| 171 |
return DECLINED; |
| 172 |
} |
| 173 |
if (r->method_number != M_POST) { |
| 174 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 175 |
"Upload Progress: Non-POST request in trackable location: %s.", r->uri); |
| 176 |
return DECLINED; |
| 177 |
} |
| 178 |
|
| 179 |
int param_error; |
| 180 |
const char *id = get_progress_id(r, ¶m_error); |
| 181 |
|
| 182 |
if (id) { |
| 183 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 184 |
"Upload Progress: Upload id='%s' in trackable location: %s.", id, r->uri); |
| 185 |
|
| 186 |
upload_progress_req_t *reqinfo = (upload_progress_req_t *)apr_pcalloc(r->pool, |
| 187 |
sizeof(upload_progress_req_t)); |
| 188 |
upload_progress_node_t *node = NULL; |
| 189 |
if (reqinfo) { |
| 190 |
reqinfo->cache_updated_at = apr_time_now(); |
| 191 |
upload_progress_node_t *node = &(reqinfo->node); |
| 192 |
|
| 193 |
strncpy(node->key, id, ARG_MAXLEN_PROGRESSID); |
| 194 |
time_t t = time(NULL); |
| 195 |
|
| 196 |
node->received = 0; |
| 197 |
node->done = 0; |
| 198 |
node->err_status = 0; |
| 199 |
node->started_at = t; |
| 200 |
node->speed = 0; |
| 201 |
node->updated_at = t; |
| 202 |
const char *content_length = apr_table_get(r->headers_in, "Content-Length"); |
| 203 |
node->length = 1; |
| 204 |
/* Content-Length is missing is case of chunked transfer encoding */ |
| 205 |
if (content_length) |
| 206 |
sscanf(content_length, "%" APR_OFF_T_FMT, &(node->length)); |
| 207 |
} |
| 208 |
|
| 209 |
ap_set_module_config(r->request_config, &upload_progress_module, reqinfo); |
| 210 |
apr_pool_cleanup_register(r->pool, r, upload_progress_cleanup, apr_pool_cleanup_null); |
| 211 |
ap_add_input_filter("UPLOAD_PROGRESS", NULL, r, r->connection); |
| 212 |
|
| 213 |
upload_progress_cache_write(server, node); |
| 214 |
|
| 215 |
} else if (param_error < 0) { |
| 216 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 217 |
"Upload Progress: Upload with invalid ID in trackable location: %s.", r->uri); |
| 218 |
/* |
| 219 |
return HTTP_BAD_REQUEST; |
| 220 |
return HTTP_NOT_FOUND; |
| 221 |
*/ |
| 222 |
|
| 223 |
} else { |
| 224 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 225 |
"Upload Progress: Upload without ID in trackable location: %s.", r->uri); |
| 226 |
} |
| 227 |
|
| 228 |
return DECLINED; |
| 229 |
} |
| 230 |
|
| 231 |
static const char *report_upload_progress_cmd(cmd_parms *cmd, void *config, int arg) |
| 232 |
{ |
| 233 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "report_upload_progress_cmd()"); |
| 234 |
DirConfig *dir = (DirConfig *)config; |
| 235 |
dir->report_enabled = arg ? 1 : -1; |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
|
| 239 |
static const char *track_upload_progress_cmd(cmd_parms *cmd, void *config, int arg) |
| 240 |
{ |
| 241 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "track_upload_progress_cmd()"); |
| 242 |
DirConfig *dir = (DirConfig *)config; |
| 243 |
dir->track_enabled = arg ? 1 : -1; |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
static const char* upload_progress_shared_memory_size_cmd(cmd_parms *cmd, |
| 248 |
void *dummy, const char *arg) |
| 249 |
{ |
| 250 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_shared_memory_size_cmd()"); |
| 251 |
ServerConfig *config = get_server_config(cmd->server); |
| 252 |
|
| 253 |
long long int n = atoi(arg); |
| 254 |
|
| 255 |
if (n <= 0) { |
| 256 |
return "UploadProgressSharedMemorySize should be positive"; |
| 257 |
} |
| 258 |
|
| 259 |
config->cache_bytes = (apr_size_t)n; |
| 260 |
|
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
static void *upload_progress_create_dir_config(apr_pool_t *p, char *dirspec) |
| 265 |
{ |
| 266 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_create_dir_config()"); |
| 267 |
DirConfig *dir = (DirConfig *)apr_pcalloc(p, sizeof(DirConfig)); |
| 268 |
dir->track_enabled = 0; |
| 269 |
dir->report_enabled = 0; |
| 270 |
return dir; |
| 271 |
} |
| 272 |
|
| 273 |
static void *upload_progress_merge_dir_config(apr_pool_t *p, void *basev, void *overridev) |
| 274 |
{ |
| 275 |
DirConfig *new = (DirConfig *)apr_pcalloc(p, sizeof(DirConfig)); |
| 276 |
DirConfig *override = (DirConfig *)overridev; |
| 277 |
DirConfig *base = (DirConfig *)basev; |
| 278 |
new->track_enabled = (override->track_enabled == 0) ? base->track_enabled : |
| 279 |
(override->track_enabled > 0 ? 1 : -1); |
| 280 |
new->report_enabled = (override->report_enabled == 0) ? base->report_enabled : |
| 281 |
(override->report_enabled > 0 ? 1 : -1); |
| 282 |
return new; |
| 283 |
} |
| 284 |
|
| 285 |
static void *upload_progress_create_server_config(apr_pool_t *p, server_rec *s) |
| 286 |
{ |
| 287 |
if (!global_server) global_server = s; |
| 288 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_create_server_config()"); |
| 289 |
ServerConfig *config = (ServerConfig *)apr_pcalloc(p, sizeof(ServerConfig)); |
| 290 |
config->cache_file = apr_pstrdup(p, CACHE_FILENAME); |
| 291 |
config->cache_bytes = 51200; |
| 292 |
return config; |
| 293 |
} |
| 294 |
|
| 295 |
static void *upload_progress_merge_server_config(apr_pool_t *p, void *basev, |
| 296 |
void *overridesv) |
| 297 |
{ |
| 298 |
return basev; |
| 299 |
} |
| 300 |
|
| 301 |
static int read_request_status(request_rec *r) |
| 302 |
{ |
| 303 |
int status; |
| 304 |
|
| 305 |
if (r) { |
| 306 |
/* error status rendered in status line is preferred because passenger |
| 307 |
clobbers request_rec->status when exception occurs */ |
| 308 |
/* FIXME: Shouldn't we read r->status instead as it's already preparsed? */ |
| 309 |
status = r->status_line ? atoi(r->status_line) : 0; |
| 310 |
if (!ap_is_HTTP_VALID_RESPONSE(status)) |
| 311 |
status = r->status; |
| 312 |
return status; |
| 313 |
} else { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
static int track_upload_progress(ap_filter_t *f, apr_bucket_brigade *bb, |
| 319 |
ap_input_mode_t mode, apr_read_type_e block, |
| 320 |
apr_off_t readbytes) |
| 321 |
{ |
| 322 |
server_rec *server = f->r->server; |
| 323 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "track_upload_progress()"); |
| 324 |
|
| 325 |
apr_status_t rv = ap_get_brigade(f->next, bb, mode, block, readbytes); |
| 326 |
|
| 327 |
upload_progress_req_t *reqinfo = get_request_config(f->r); |
| 328 |
if (!reqinfo) { |
| 329 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "Request config not available"); |
| 330 |
return rv; |
| 331 |
} |
| 332 |
upload_progress_node_t *node = &(reqinfo->node); |
| 333 |
|
| 334 |
time_t t = time(NULL); |
| 335 |
node->updated_at = t; |
| 336 |
if (rv == APR_SUCCESS) { |
| 337 |
apr_off_t length; |
| 338 |
apr_brigade_length(bb, 1, &length); |
| 339 |
node->received += length; |
| 340 |
if (node->received > node->length) /* handle chunked transfer */ |
| 341 |
node->length = node->received; |
| 342 |
int upload_time = t - node->started_at; |
| 343 |
if (upload_time > 0) { |
| 344 |
node->speed = node->received / upload_time; |
| 345 |
} |
| 346 |
} else { |
| 347 |
node->err_status = read_request_status(f->r); |
| 348 |
reqinfo->cache_updated_at = 0; /* force cache update */ |
| 349 |
} |
| 350 |
|
| 351 |
apr_time_t now = apr_time_now(); |
| 352 |
if ((now - reqinfo->cache_updated_at) > apr_time_from_msec(500)) { |
| 353 |
reqinfo->cache_updated_at = now; |
| 354 |
upload_progress_cache_write(server, node); |
| 355 |
} |
| 356 |
|
| 357 |
return rv; |
| 358 |
} |
| 359 |
|
| 360 |
static int check_request_argument(const char *value, int len, char *allowed, int minlen, int maxlen) { |
| 361 |
/* Check the length of the argument */ |
| 362 |
if (len > maxlen) return -1; |
| 363 |
if (len < minlen) return -2; |
| 364 |
/* If no whitelist given, assume everything whitelisted */ |
| 365 |
if (!allowed) return 0; |
| 366 |
/* Check each char to be in the whitelist */ |
| 367 |
if (strspn(value, allowed) < len) return -3; |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
static char *get_param_value(char *p, const char *param_name, int *len) { |
| 372 |
char pn1[3] = {toupper(param_name[0]), tolower(param_name[0]), 0}; |
| 373 |
int pn_len = strlen(param_name); |
| 374 |
char *val_end; |
| 375 |
static char *param_sep = "&"; |
| 376 |
|
| 377 |
while (p) { |
| 378 |
if ((strncasecmp(p, param_name, pn_len) == 0) && (p[pn_len] == '=')) |
| 379 |
break; |
| 380 |
if (*p) p++; |
| 381 |
p = strpbrk(p, pn1); |
| 382 |
} |
| 383 |
if (p) { |
| 384 |
p += (pn_len + 1); |
| 385 |
*len = strcspn(p, param_sep); |
| 386 |
} |
| 387 |
return p; |
| 388 |
} |
| 389 |
|
| 390 |
static const char *get_progress_id(request_rec *r, int *param_error) { |
| 391 |
int len; |
| 392 |
|
| 393 |
/* try to find progress id in http headers */ |
| 394 |
const char *id = apr_table_get(r->headers_in, PROGRESS_ID); |
| 395 |
if (id) { |
| 396 |
*param_error = check_request_argument(id, strlen(id), ARG_ALLOWED_PROGRESSID, |
| 397 |
ARG_MINLEN_PROGRESSID, ARG_MAXLEN_PROGRESSID); |
| 398 |
if (*param_error) return NULL; |
| 399 |
return id; |
| 400 |
} |
| 401 |
|
| 402 |
/* if progress id not found in headers, check request args (query string) */ |
| 403 |
id = get_param_value(r->args, PROGRESS_ID, &len); |
| 404 |
if (id) { |
| 405 |
*param_error = check_request_argument(id, len, ARG_ALLOWED_PROGRESSID, |
| 406 |
ARG_MINLEN_PROGRESSID, ARG_MAXLEN_PROGRESSID); |
| 407 |
if (*param_error) return NULL; |
| 408 |
return apr_pstrndup(r->connection->pool, id, len); |
| 409 |
} |
| 410 |
|
| 411 |
*param_error = 1; /* not found */ |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
|
| 415 |
static const char *get_json_callback_param(request_rec *r, int *param_error) { |
| 416 |
char *val; |
| 417 |
int len; |
| 418 |
|
| 419 |
val = get_param_value(r->args, JSON_CB_PARAM, &len); |
| 420 |
if (val) { |
| 421 |
*param_error = check_request_argument(val, len, ARG_ALLOWED_JSONPCALLBACK, |
| 422 |
ARG_MINLEN_JSONPCALLBACK, ARG_MAXLEN_JSONPCALLBACK); |
| 423 |
if (*param_error) return NULL; |
| 424 |
return apr_pstrndup(r->connection->pool, val, len); |
| 425 |
} |
| 426 |
|
| 427 |
*param_error = 1; /* not found */ |
| 428 |
return NULL; |
| 429 |
} |
| 430 |
|
| 431 |
static inline int check_node(upload_progress_node_t *node, const char *key) { |
| 432 |
return ((node) && (strncasecmp(node->key, key, ARG_MAXLEN_PROGRESSID) == 0)) ? 1 : 0; |
| 433 |
} |
| 434 |
|
| 435 |
static upload_progress_node_t *find_node(server_rec *server, const char *key) { |
| 436 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "find_node()"); |
| 437 |
|
| 438 |
ServerConfig *config = get_server_config(server); |
| 439 |
upload_progress_cache_t *cache = config->cache; |
| 440 |
upload_progress_node_t *node, *nodes = config->nodes; |
| 441 |
int *list = config->list; |
| 442 |
int active = cache->active; |
| 443 |
int i; |
| 444 |
|
| 445 |
for (i = 0; i < active; i++) { |
| 446 |
node = &nodes[list[i]]; |
| 447 |
if (check_node(node, key)) |
| 448 |
return node; |
| 449 |
} |
| 450 |
return NULL; |
| 451 |
} |
| 452 |
|
| 453 |
static apr_status_t upload_progress_cleanup(void *data) |
| 454 |
{ |
| 455 |
request_rec *r = (request_rec *)data; |
| 456 |
server_rec *server = r->server; |
| 457 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "upload_progress_cleanup()"); |
| 458 |
|
| 459 |
upload_progress_req_t *reqinfo = get_request_config(r); |
| 460 |
if (!reqinfo) { |
| 461 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "Request config not available"); |
| 462 |
return APR_SUCCESS; |
| 463 |
} |
| 464 |
upload_progress_node_t *node = &(reqinfo->node); |
| 465 |
node->err_status = read_request_status(r); |
| 466 |
node->updated_at = time(NULL); |
| 467 |
node->done = 1; |
| 468 |
upload_progress_cache_write(server, node); |
| 469 |
|
| 470 |
return APR_SUCCESS; |
| 471 |
} |
| 472 |
|
| 473 |
static void upload_progress_cache_write(server_rec *server, upload_progress_node_t *new_data) |
| 474 |
{ |
| 475 |
if (!new_data) return; |
| 476 |
ServerConfig *config = get_server_config(server); |
| 477 |
|
| 478 |
CACHE_LOCK(); |
| 479 |
upload_progress_node_t *node = find_node(server, new_data->key); |
| 480 |
time_t now = time(NULL); |
| 481 |
#define EXPIRED(node,now) ((now - node->updated_at) > PROGRESS_EXPIRES) |
| 482 |
if (node) { |
| 483 |
/* reuse node if: request finished, expired, or same started_at as node */ |
| 484 |
if (node->started_at == new_data->started_at || node->done || EXPIRED(node, now)) { |
| 485 |
memcpy(node, new_data, sizeof(upload_progress_node_t)); |
| 486 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 487 |
"Upload Progress: Reused existing node with id='%" UP_ID_FMT "'.", node->key); |
| 488 |
} else { |
| 489 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 490 |
"Upload Progress: Upload with id='%" UP_ID_FMT "' already exists, ignoring.", node->key); |
| 491 |
} |
| 492 |
} else { |
| 493 |
|
| 494 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "cleaning expired cache nodes"); |
| 495 |
|
| 496 |
upload_progress_cache_t *cache = config->cache; |
| 497 |
upload_progress_node_t *nodes = config->nodes; |
| 498 |
int *list = config->list; |
| 499 |
int i, tmp; |
| 500 |
|
| 501 |
/* check active nodes list, if expired node found |
| 502 |
then swap it with last active and shrink active list by one */ |
| 503 |
for (i = 0; i < cache->active; i++) { |
| 504 |
node = &nodes[list[i]]; |
| 505 |
if (EXPIRED(node, now)) { |
| 506 |
cache->active -= 1; |
| 507 |
tmp = list[cache->active]; |
| 508 |
list[cache->active] = list[i]; |
| 509 |
list[i] = tmp; |
| 510 |
i--; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
if (cache->active < cache->count) { |
| 515 |
node = &config->nodes[config->list[cache->active]]; |
| 516 |
cache->active += 1; |
| 517 |
memcpy(node, new_data, sizeof(upload_progress_node_t)); |
| 518 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 519 |
"Upload Progress: Added upload with id='%" UP_ID_FMT "' to list.", node->key); |
| 520 |
} else { |
| 521 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cache full"); |
| 522 |
} |
| 523 |
|
| 524 |
} |
| 525 |
CACHE_UNLOCK(); |
| 526 |
|
| 527 |
} |
| 528 |
|
| 529 |
static apr_status_t upload_progress_cache_init(apr_pool_t *pool, ServerConfig *config) |
| 530 |
{ |
| 531 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_cache_init()"); |
| 532 |
|
| 533 |
apr_status_t result; |
| 534 |
apr_size_t size; |
| 535 |
int nodes_cnt, i; |
| 536 |
|
| 537 |
if (config->cache_file) { |
| 538 |
/* Remove any existing shm segment with this name. */ |
| 539 |
apr_shm_remove(config->cache_file, pool); |
| 540 |
unlink(config->cache_file); |
| 541 |
} |
| 542 |
|
| 543 |
size = APR_ALIGN_DEFAULT(config->cache_bytes); |
| 544 |
result = apr_shm_create(&config->cache_shm, size, config->cache_file, pool); |
| 545 |
if (result != APR_SUCCESS) { |
| 546 |
return result; |
| 547 |
} |
| 548 |
|
| 549 |
/* Determine the usable size of the shm segment. */ |
| 550 |
size = apr_shm_size_get(config->cache_shm); |
| 551 |
nodes_cnt = (size - sizeof(upload_progress_cache_t)) / |
| 552 |
(sizeof(int) + sizeof(upload_progress_node_t)); |
| 553 |
|
| 554 |
/* init cache */ |
| 555 |
config->cache = (upload_progress_cache_t *)apr_shm_baseaddr_get(config->cache_shm); |
| 556 |
config->list = (int *)(config->cache + 1); |
| 557 |
config->nodes = (upload_progress_node_t *)(config->list + nodes_cnt); |
| 558 |
config->cache->count = nodes_cnt; |
| 559 |
config->cache->active = 0; |
| 560 |
for (i = 0; i < nodes_cnt; i++) config->list[i] = i; |
| 561 |
|
| 562 |
ap_log_error(APLOG_MARK, APLOG_INFO, 0, global_server, |
| 563 |
"Upload Progress: monitoring max %i simultaneous uploads, id (%s) length %i..%i", |
| 564 |
nodes_cnt, PROGRESS_ID, ARG_MINLEN_PROGRESSID, ARG_MAXLEN_PROGRESSID); |
| 565 |
|
| 566 |
return APR_SUCCESS; |
| 567 |
} |
| 568 |
|
| 569 |
static int upload_progress_init(apr_pool_t *p, apr_pool_t *plog, |
| 570 |
apr_pool_t *ptemp, |
| 571 |
server_rec *s) { |
| 572 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, s, "upload_progress_init()"); |
| 573 |
|
| 574 |
apr_status_t result; |
| 575 |
|
| 576 |
ServerConfig *config = get_server_config(s); |
| 577 |
|
| 578 |
void *data; |
| 579 |
const char *userdata_key = "upload_progress_init"; |
| 580 |
|
| 581 |
/* upload_progress_init will be called twice. Don't bother |
| 582 |
* going through all of the initialization on the first call |
| 583 |
* because it will just be thrown away.*/ |
| 584 |
apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 585 |
if (!data) { |
| 586 |
apr_pool_userdata_set((const void *)1, userdata_key, |
| 587 |
apr_pool_cleanup_null, s->process->pool); |
| 588 |
|
| 589 |
/* If the cache file already exists then delete it. Otherwise we are |
| 590 |
* going to run into problems creating the shared memory. */ |
| 591 |
if (config->cache_file) { |
| 592 |
char *lck_file = apr_pstrcat(ptemp, config->cache_file, ".lck", |
| 593 |
NULL); |
| 594 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, s, "Upload Progress: Remove lock file"); |
| 595 |
apr_file_remove(lck_file, ptemp); |
| 596 |
} |
| 597 |
return OK; |
| 598 |
} |
| 599 |
|
| 600 |
/* initializing cache if shared memory size is not zero and we already |
| 601 |
* don't have shm address |
| 602 |
*/ |
| 603 |
if (!config->cache_shm && config->cache_bytes > 0) { |
| 604 |
result = upload_progress_cache_init(p, config); |
| 605 |
if (result != APR_SUCCESS) { |
| 606 |
ap_log_error(APLOG_MARK, APLOG_ERR, result, s, |
| 607 |
"Upload Progress cache: could not create shared memory segment"); |
| 608 |
return DONE; |
| 609 |
} |
| 610 |
|
| 611 |
if (config->cache_file) { |
| 612 |
config->lock_file = apr_pstrcat(p, config->cache_file, ".lck", |
| 613 |
NULL); |
| 614 |
} |
| 615 |
|
| 616 |
result = apr_global_mutex_create(&config->cache_lock, |
| 617 |
config->lock_file, APR_LOCK_DEFAULT, |
| 618 |
p); |
| 619 |
if (result != APR_SUCCESS) { |
| 620 |
return result; |
| 621 |
} |
| 622 |
|
| 623 |
#ifdef AP_NEED_SET_MUTEX_PERMS |
| 624 |
result = ap_unixd_set_global_mutex_perms(config->cache_lock); |
| 625 |
if (result != APR_SUCCESS) { |
| 626 |
ap_log_error(APLOG_MARK, APLOG_CRIT, result, s, |
| 627 |
"Upload progress cache: failed to set mutex permissions"); |
| 628 |
return result; |
| 629 |
} |
| 630 |
#endif |
| 631 |
|
| 632 |
} else { |
| 633 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
| 634 |
"Upload Progress cache: cache size is zero, disabling " |
| 635 |
"shared memory cache"); |
| 636 |
} |
| 637 |
|
| 638 |
return(OK); |
| 639 |
} |
| 640 |
|
| 641 |
static int reportuploads_handler(request_rec *r) |
| 642 |
{ |
| 643 |
server_rec *server = r->server; |
| 644 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, server, "reportuploads_handler()"); |
| 645 |
|
| 646 |
upload_progress_node_t upload, *found; |
| 647 |
int param_error; |
| 648 |
char *response; |
| 649 |
DirConfig *dir = get_dir_config(r); |
| 650 |
|
| 651 |
if (!dir || (dir->report_enabled <= 0)) { |
| 652 |
return DECLINED; |
| 653 |
} |
| 654 |
if (r->method_number != M_GET) { |
| 655 |
return HTTP_METHOD_NOT_ALLOWED; |
| 656 |
} |
| 657 |
|
| 658 |
/* get the tracking id if any */ |
| 659 |
const char *id = get_progress_id(r, ¶m_error); |
| 660 |
|
| 661 |
if (id == NULL) { |
| 662 |
if (param_error < 0) { |
| 663 |
ap_log_error(APLOG_MARK, APLOG_INFO, 0, server, |
| 664 |
"Upload Progress: Report requested with invalid id. uri=%s", r->uri); |
| 665 |
return HTTP_BAD_REQUEST; |
| 666 |
} else { |
| 667 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 668 |
"Upload Progress: Report requested without id. uri=%s", r->uri); |
| 669 |
return HTTP_NOT_FOUND; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 674 |
"Upload Progress: Report requested with id='%s'. uri=%s", id, r->uri); |
| 675 |
|
| 676 |
ServerConfig *config = get_server_config(server); |
| 677 |
|
| 678 |
CACHE_LOCK(); |
| 679 |
found = find_node(server, id); |
| 680 |
if (found) { |
| 681 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 682 |
"Node with id=%s found for report", id); |
| 683 |
memcpy(&upload, found, sizeof(upload)); |
| 684 |
} else { |
| 685 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 686 |
"Node with id=%s not found for report", id); |
| 687 |
} |
| 688 |
CACHE_UNLOCK(); |
| 689 |
|
| 690 |
ap_set_content_type(r, "application/json"); |
| 691 |
|
| 692 |
apr_table_set(r->headers_out, "Expires", "Mon, 28 Sep 1970 06:00:00 GMT"); |
| 693 |
apr_table_set(r->headers_out, "Cache-Control", "no-cache"); |
| 694 |
|
| 695 |
/* There are 4 possibilities |
| 696 |
* request not yet started: found = false |
| 697 |
* request in error: err_status >= NGX_HTTP_SPECIAL_RESPONSE |
| 698 |
* request finished: done = true |
| 699 |
* request not yet started but registered: length==0 && rest ==0 |
| 700 |
* request in progress: rest > 0 |
| 701 |
*/ |
| 702 |
|
| 703 |
if (!found) { |
| 704 |
response = apr_psprintf(r->pool, "{ \"state\" : \"starting\", \"uuid\" : \"%s\" }", id); |
| 705 |
} else if (upload.err_status >= HTTP_BAD_REQUEST ) { |
| 706 |
response = apr_psprintf(r->pool, "{ " |
| 707 |
"\"state\": \"error\", " |
| 708 |
"\"status\": %d, " |
| 709 |
"\"uuid\": \"%s\" " |
| 710 |
"}", upload.err_status, id); |
| 711 |
} else if (upload.done) { |
| 712 |
response = apr_psprintf(r->pool, "{ " |
| 713 |
"\"state\": \"done\", " |
| 714 |
"\"size\": %" APR_OFF_T_FMT ", " |
| 715 |
"\"speed\": %" APR_OFF_T_FMT ", " |
| 716 |
"\"started_at\": %li, " |
| 717 |
"\"completed_at\": %li, " |
| 718 |
"\"uuid\": \"%s\" " |
| 719 |
"}", upload.length, upload.speed, upload.started_at, upload.updated_at, id); |
| 720 |
} else if (upload.received == 0) { |
| 721 |
response = apr_psprintf(r->pool, "{ \"state\" : \"starting\", \"uuid\" : \"%s\" }", id); |
| 722 |
} else { |
| 723 |
time_t eta = 0, t = time(NULL); |
| 724 |
if (upload.speed > 0) eta = upload.started_at + upload.length / upload.speed; |
| 725 |
if (eta <= t) eta = t + 1; |
| 726 |
response = apr_psprintf(r->pool, "{ " |
| 727 |
"\"state\": \"uploading\", " |
| 728 |
"\"received\": %" APR_OFF_T_FMT ", " |
| 729 |
"\"size\": %" APR_OFF_T_FMT ", " |
| 730 |
"\"speed\": %" APR_OFF_T_FMT ", " |
| 731 |
"\"started_at\": %li, " |
| 732 |
"\"eta\": %li, " |
| 733 |
"\"uuid\": \"%s\" " |
| 734 |
"}", upload.received, upload.length, upload.speed, upload.started_at, eta, id); |
| 735 |
} |
| 736 |
|
| 737 |
char *completed_response; |
| 738 |
|
| 739 |
/* get the jsonp callback if any */ |
| 740 |
const char *jsonp = get_json_callback_param(r, ¶m_error); |
| 741 |
|
| 742 |
if (param_error < 0) { |
| 743 |
ap_log_error(APLOG_MARK, APLOG_INFO, 0, server, |
| 744 |
"Upload Progress: Report requested with invalid JSON-P callback. uri=%s", r->uri); |
| 745 |
return HTTP_BAD_REQUEST; |
| 746 |
} |
| 747 |
|
| 748 |
up_log(APLOG_MARK, APLOG_DEBUG, 0, server, |
| 749 |
"Upload Progress: JSON-P callback: %s.", jsonp); |
| 750 |
|
| 751 |
// fix up response for jsonp request, if needed |
| 752 |
if (jsonp) { |
| 753 |
completed_response = apr_psprintf(r->pool, "%s(%s);\r\n", jsonp, response); |
| 754 |
} else { |
| 755 |
completed_response = apr_psprintf(r->pool, "%s\r\n", response); |
| 756 |
} |
| 757 |
|
| 758 |
ap_rputs(completed_response, r); |
| 759 |
|
| 760 |
return OK; |
| 761 |
} |
| 762 |
|
| 763 |
static void upload_progress_child_init(apr_pool_t *p, server_rec *s) |
| 764 |
{ |
| 765 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, s, "upload_progress_child_init()"); |
| 766 |
|
| 767 |
apr_status_t rv; |
| 768 |
ServerConfig *config = get_server_config(s); |
| 769 |
|
| 770 |
if (!config->cache_lock) { |
| 771 |
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, "Global mutex not set."); |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
rv = apr_global_mutex_child_init(&config->cache_lock, config->lock_file, p); |
| 776 |
if (rv != APR_SUCCESS) { |
| 777 |
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, |
| 778 |
"Failed to initialise global mutex %s in child process %" |
| 779 |
APR_PID_T_FMT ".", config->lock_file, getpid()); |
| 780 |
} |
| 781 |
|
| 782 |
if (!config->cache_shm) { |
| 783 |
rv = apr_shm_attach(&config->cache_shm, config->cache_file, p); |
| 784 |
if (rv != APR_SUCCESS) { |
| 785 |
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, "Failed to attach to " |
| 786 |
"shared memory file '%s'", config->cache_file); |
| 787 |
return; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
config->cache = (upload_progress_cache_t *)apr_shm_baseaddr_get(config->cache_shm); |
| 792 |
config->list = (int *)(config->cache + 1); |
| 793 |
config->nodes = (upload_progress_node_t *)(config->list + config->cache->count); |
| 794 |
} |
| 795 |
|
| 796 |
static const command_rec upload_progress_cmds[] = |
| 797 |
{ |
| 798 |
AP_INIT_FLAG("TrackUploads", track_upload_progress_cmd, NULL, OR_AUTHCFG, |
| 799 |
"Track upload progress in this location"), |
| 800 |
AP_INIT_FLAG("ReportUploads", report_upload_progress_cmd, NULL, OR_AUTHCFG, |
| 801 |
"Report upload progress in this location"), |
| 802 |
AP_INIT_TAKE1("UploadProgressSharedMemorySize", upload_progress_shared_memory_size_cmd, NULL, RSRC_CONF, |
| 803 |
"Size of shared memory used to keep uploads data, default 100KB"), |
| 804 |
{ NULL } |
| 805 |
}; |
| 806 |
|
| 807 |
static void upload_progress_register_hooks (apr_pool_t *p) |
| 808 |
{ |
| 809 |
/**/up_log(APLOG_MARK, APLOG_DEBUG, 0, global_server, "upload_progress_register_hooks()"); |
| 810 |
|
| 811 |
ap_hook_fixups(upload_progress_handle_request, NULL, NULL, APR_HOOK_FIRST); |
| 812 |
ap_hook_handler(reportuploads_handler, NULL, NULL, APR_HOOK_FIRST); |
| 813 |
ap_hook_post_config(upload_progress_init, NULL, NULL, APR_HOOK_MIDDLE); |
| 814 |
ap_hook_child_init(upload_progress_child_init, NULL, NULL, APR_HOOK_MIDDLE); |
| 815 |
ap_register_input_filter("UPLOAD_PROGRESS", track_upload_progress, NULL, AP_FTYPE_RESOURCE); |
| 816 |
} |
| 817 |
|
| 818 |
module AP_MODULE_DECLARE_DATA upload_progress_module = |
| 819 |
{ |
| 820 |
STANDARD20_MODULE_STUFF, |
| 821 |
upload_progress_create_dir_config, |
| 822 |
upload_progress_merge_dir_config, |
| 823 |
upload_progress_create_server_config, |
| 824 |
upload_progress_merge_server_config, |
| 825 |
upload_progress_cmds, |
| 826 |
upload_progress_register_hooks, /* callback for registering hooks */ |
| 827 |
}; |