Project

General

Profile

Feature #1632 » mod_uploadprogress.c.diff

little fix - icy, 2008-04-10 10:13

View differences:

mod_uploadprogress.c 2008-04-10 12:12:35.000000000 +0200
21 21
 * Initial: Jan Kneschke <jan@kneschke.de>
22 22
 * Timeout+Status addon: Bjoern Kalkbrenner <terminar@cyberphoria.org> [20070112]
23 23
 *
24
 * the timeout is used to keep in the status information intact even if the parent 
24
 * the timeout is used to keep in the status information intact even if the parent
25 25
 * connection is gone already
26 26
 */
27 27

  
......
31 31

  
32 32
	time_t timeout;
33 33
	int status;
34
	off_t size;
34 35
} connection_map_entry;
35 36

  
36 37
typedef struct {
......
140 141
	return NULL;
141 142
}
142 143

  
143
static int connection_map_remove_connection(connection_map *cm, connection_map_entry *entry) {
144
	size_t i;
145

  
146
	for (i = 0; i < cm->used; i++) {
147
		connection_map_entry *cme = cm->ptr[i];
144
static void connection_map_remove_connection(connection_map *cm, size_t i) {
145
	connection_map_entry *cme = cm->ptr[i];
148 146

  
149
		if (cme == entry) {
150
			/* found connection */
151
			buffer_reset(cme->tracking_id);
152
			cme->timeout=0;
153
			cme->status=0;
154

  
155
			cm->used--;
156

  
157
			/* swap positions with the last entry */
158
			if (cm->used) {
159
				cm->ptr[i] = cm->ptr[cm->used];
160
				cm->ptr[cm->used] = cme;
161
			}
162

  
163
			return 1;
164
		}
147
	buffer_reset(cme->tracking_id);
148
	cme->timeout=0;
149
	cme->status=0;
150

  
151
	cm->used--;
152

  
153
	/* swap positions with the last entry */
154
	if (cm->used) {
155
		cm->ptr[i] = cm->ptr[cm->used];
156
		cm->ptr[cm->used] = cme;
165 157
	}
166

  
167
	return 0;
168 158
}
169 159

  
170 160
/**
171
 * remove dead tracking IDs 
161
 * remove dead tracking IDs
172 162
 *
173
 * uploadprogress.remove-timeout sets a grace-period in which the 
174
 * connection status is still known even of the connection is already 
163
 * uploadprogress.remove-timeout sets a grace-period in which the
164
 * connection status is still known even of the connection is already
175 165
 * being removed
176 166
 *
177 167
 */
......
184 174

  
185 175
		if (cme->timeout != 0 && cme->timeout < now_t) {
186 176
			/* found connection */
187
			connection_map_remove_connection(cm, cme);
177
			connection_map_remove_connection(cm, i);
188 178
		}
189 179
	}
190 180
}
191 181

  
192
/** 
193
 * extract the tracking-id from the parameters 
182
/**
183
 * extract the tracking-id from the parameters
194 184
 *
195 185
 * for POST requests it is part of the request headers
196 186
 * for GET requests ... too
......
234 224
			}
235 225

  
236 226
			eq = strchr(p->tmp_buf->ptr, '=');
237
			
227

  
238 228
			if (eq) {
239 229
				*eq = '\0';
240 230

  
......
244 234
					/* found */
245 235

  
246 236
					buffer_copy_string_len(p->tmp_buf, start + key_len + 1, var_len - key_len - 1);
247
		
237

  
248 238
					b = p->tmp_buf;
249 239

  
250 240
					break;
251
				} 
241
				}
252 242
			}
253 243
		} while (amp);
254 244

  
......
417 407

  
418 408
	mod_uploadprogress_patch_connection(srv, con, p);
419 409

  
420
	/* no progress URL set, ignore request */	
410
	/* no progress URL set, ignore request */
421 411
	if (buffer_is_empty(p->conf.progress_url)) return HANDLER_GO_ON;
422 412

  
423 413
	switch(con->request.http_method) {
......
425 415
		/**
426 416
		 * a POST request is the UPLOAD itself
427 417
		 *
428
		 * get the unique tracker id 
418
		 * get the unique tracker id
429 419
		 */
430 420
		if (NULL == (tracking_id = get_tracking_id(p, con))) {
431 421
			return HANDLER_GO_ON;
......
433 423

  
434 424
		if (NULL == (map_con_entry = connection_map_get_connection_entry(p->con_map, tracking_id))) {
435 425
			connection_map_insert(p->con_map, tracking_id, con);
436
		
426

  
437 427
			if (p->conf.debug) TRACE("POST: connection is new, registered: %s", BUF_STR(tracking_id));
438 428
		} else {
439 429
			map_con_entry->timeout = 0;
440 430
			map_con_entry->status = 0;
441
			
431

  
442 432
			if (p->conf.debug) TRACE("POST: connection is known, id: %s", BUF_STR(tracking_id));
443 433
		}
444 434

  
......
481 471
			/**
482 472
			 * looks like we don't know the tracking id yet, GET and POST out of sync ? */
483 473
			BUFFER_APPEND_STRING_CONST(b, "new Object({ 'state' : 'starting' })\r\n");
484
			
474

  
485 475
			if (p->conf.debug) TRACE("connection unknown: %s, sending: %s", BUF_STR(tracking_id), BUF_STR(b));
486 476

  
487 477
			return HANDLER_FINISHED;
488 478
		}
489 479

  
490 480
		BUFFER_COPY_STRING_CONST(b, "new Object({ 'state' : ");
491
	
481

  
492 482
		if (post_con_entry->status == 413) {
493
			/* the upload was too large */	
483
			/* the upload was too large */
494 484
			BUFFER_APPEND_STRING_CONST(b, "'error', 'status' : 413");
495 485
		} else if (post_con_entry->con == NULL) {
496 486
			/* the connection is already gone */
497
			buffer_append_string(b, "'done'");
487
			buffer_append_string(b, "'done', 'size' : ");
488
			buffer_append_off_t(b, post_con_entry->size);
498 489
		} else {
499 490
			/* the upload is already done, but the connection might be still open */
500 491
			buffer_append_string(b, post_con_entry->con->recv->is_closed ? "'done'" : "'uploading'");
......
545 536

  
546 537
	if (NULL == (map_con_entry = connection_map_get_connection_entry(p->con_map, tracking_id))) {
547 538
		/**
548
		 * in case the request parser meant the request was too large the URI handler won't 
539
		 * in case the request parser meant the request was too large the URI handler won't
549 540
		 * get called. Insert the connection mapping here
550 541
		 */
551 542
		if (NULL == (map_con_entry = connection_map_insert(p->con_map, tracking_id, con))) {
......
555 546

  
556 547
	/* ok, found our entries, setting 413 here for status */
557 548
	map_con_entry->status = 413;
558
	
549

  
559 550
	return HANDLER_GO_ON;
560 551
}
561 552

  
......
573 564
	UNUSED(srv);
574 565

  
575 566
	if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
576
	
567

  
577 568
	/*
578 569
	 * only need to handle the upload request.
579 570
	 */
......
601 592
		return HANDLER_GO_ON;
602 593
	}
603 594

  
595
	// save request size to be able to report it even when cm->con == NULL
596
	cm->size = con->request.content_length;
597

  
604 598
	cm->timeout = time(NULL) + p->conf.remove_timeout;
605 599
	cm->con     = NULL; /* con becomes invalid very soon */
606 600

  
......
608 602
}
609 603

  
610 604
/**
611
 * remove dead connections once in while 
605
 * remove dead connections once in while
612 606
 */
613 607
TRIGGER_FUNC(mod_uploadprogress_trigger) {
614 608
	plugin_data *p = p_d;
......
634 628
	p->cleanup     = mod_uploadprogress_free;
635 629
	p->handle_trigger = mod_uploadprogress_trigger;
636 630
	p->handle_response_header = mod_uploadprogress_response_header;
637
	
631

  
638 632
	p->data        = NULL;
639 633

  
640 634
	return 0;
(2-2/2)