You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_dtcl-cvs@tcl.apache.org by da...@apache.org on 2001/03/15 20:37:03 UTC
cvs commit: mod_dtcl apache_cookie.c apache_multipart_buffer.c apache_request.c apache_request.h mod_dtcl.c
davidw 01/03/15 11:37:02
Modified: . apache_cookie.c apache_multipart_buffer.c
apache_request.c apache_request.h mod_dtcl.c
Log:
Commited new apreq API for uploading to Tcl variables, as well as
getting tmpfile's name.
Revision Changes Path
1.3 +39 -7 mod_dtcl/apache_cookie.c
Index: apache_cookie.c
===================================================================
RCS file: /home/cvs/mod_dtcl/apache_cookie.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- apache_cookie.c 2000/12/29 13:14:05 1.2
+++ apache_cookie.c 2001/03/15 19:36:55 1.3
@@ -169,10 +169,10 @@
else {
c->values = ap_make_array(r->pool, 4, sizeof(char *));
}
-
- if (!*pair) {
+
+ if (!*pair) {
ApacheCookieAdd(c, "");
- }
+ }
while (*pair && (val = ap_getword(r->pool, &pair, '&'))) {
ap_unescape_url((char *)val);
@@ -191,9 +191,41 @@
if(val) { \
cookie_push_arr(arr, ap_pstrcat(p, name, "=", val, NULL)); \
}
+
+static char * escape_url(pool *p, char *val)
+{
+ char *result = ap_os_escape_path(p, val?val:"", 1);
+ char *end = result + strlen(result);
+ char *seek;
+
+ for ( seek = end-1; seek >= result; --seek) {
+ char *ptr, *replacement;
+
+ switch (*seek) {
+
+ case '&':
+ replacement = "%26";
+ break;
+ case '=':
+ replacement = "%3d";
+ break;
+ /* additional cases here */
+
+ default:
+ continue; /* next for() */
+ }
-#define escape_url(val) \
-ap_os_escape_path(p, val?val:"", 1)
+
+ for (ptr = end; ptr > seek; --ptr) {
+ ptr[2] = ptr[0];
+ }
+
+ strncpy(seek, replacement, 3);
+ end += 2;
+ }
+
+ return(result);
+}
char *ApacheCookie_as_string(ApacheCookie *c)
{
@@ -214,10 +246,10 @@
cookie_push_arr(values, "secure");
}
- cookie = ap_pstrcat(p, escape_url(c->name), "=", NULL);
+ cookie = ap_pstrcat(p, escape_url(p, c->name), "=", NULL);
for (i=0; i<c->values->nelts; i++) {
cookie = ap_pstrcat(p, cookie,
- escape_url(((char**)c->values->elts)[i]),
+ escape_url(p, ((char**)c->values->elts)[i]),
(i < (c->values->nelts-1) ? "&" : NULL),
NULL);
}
1.4 +1 -0 mod_dtcl/apache_multipart_buffer.c
Index: apache_multipart_buffer.c
===================================================================
RCS file: /home/cvs/mod_dtcl/apache_multipart_buffer.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- apache_multipart_buffer.c 2001/03/01 18:31:29 1.3
+++ apache_multipart_buffer.c 2001/03/15 19:36:56 1.4
@@ -182,6 +182,7 @@
/* finds a boundary */
int find_boundary(multipart_buffer *self, char *boundary)
{
+ int len, bound_len = strlen(boundary);
char *line;
/* loop thru lines */
1.4 +40 -20 mod_dtcl/apache_request.c
Index: apache_request.c
===================================================================
RCS file: /home/cvs/mod_dtcl/apache_request.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- apache_request.c 2001/03/13 12:00:44 1.3
+++ apache_request.c 2001/03/15 19:36:57 1.4
@@ -203,8 +203,9 @@
req->upload = NULL;
req->post_max = -1;
req->disable_uploads = 0;
- req->ApacheUploadHook = NULL;
- req->hookptr = NULL;
+ req->upload_hook = NULL;
+ req->hook_data = NULL;
+ req->temp_dir = NULL;
req->parsed = 0;
req->r = r;
@@ -317,20 +318,41 @@
return OK;
}
-FILE *ApacheRequest_tmpfile(ApacheRequest *req)
+static void remove_tmpfile(void *data) {
+ remove((char *) data);
+}
+
+FILE *ApacheRequest_tmpfile(ApacheRequest *req, ApacheUpload *upload)
{
request_rec *r = req->r;
FILE *fp;
-
- if (!(fp = tmpfile())) {
- ap_log_rerror(REQ_ERROR,
- "[libapreq] could not create tmpfile()");
+ char prefix[] = "apreq";
+ char *name;
+ int fd, tries = 100;
+
+ while (--tries > 0) {
+ if ( (name = tempnam(req->temp_dir, prefix)) == NULL ) continue;
+ fd = ap_popenf(r->pool, name, O_CREAT|O_EXCL|O_RDWR, 0600);
+ if ( fd >= 0 )
+ break; /* success */
+ else
+ free(name);
}
- else {
- ap_note_cleanups_for_file(r->pool, fp);
+
+ if ( tries == 0 || (fp = ap_pfdopen(r->pool, fd, "w+") ) == NULL ) {
+ ap_log_rerror(REQ_ERROR,
+ "[libapreq] could not open temp file '%s'", name);
+ if ( fd >= 0 ) { remove(name); }
+ return NULL;
}
+ upload->fp = fp;
+ upload->tempname = ap_pstrdup(r->pool, name);
+ ap_register_cleanup(r->pool, (void *)upload->tempname,
+ remove_tmpfile, ap_null_cleanup);
+
return fp;
+
}
int ApacheRequest_parse_multipart(ApacheRequest *req)
@@ -378,8 +400,7 @@
table *header = multipart_buffer_headers(mbuff);
const char *cd, *param=NULL, *filename=NULL;
char buff[FILLUNIT];
- int blen;
- int wlen;
+ int blen, wlen;
if (!header) {
return OK;
@@ -420,19 +441,18 @@
upload = ApacheUpload_new(req);
req->upload = upload;
}
- if (req->ApacheUploadHook == NULL) {
- if (!(upload->fp = ApacheRequest_tmpfile(req))) {
- return HTTP_INTERNAL_SERVER_ERROR;
- }
- }
+ if (! req->upload_hook && ! ApacheRequest_tmpfile(req, upload) ) {
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+
upload->info = header;
upload->filename = ap_pstrdup(req->r->pool, filename);
upload->name = ap_pstrdup(req->r->pool, param);
while ((blen = multipart_buffer_read(mbuff, buff, sizeof(buff)))) {
- if (req->ApacheUploadHook != NULL) {
- wlen = req->ApacheUploadHook(req->hookptr, buff, blen);
+ if (req->upload_hook != NULL) {
+ wlen = req->upload_hook(req->hook_data, buff, blen, upload);
} else {
wlen = fwrite(buff, 1, blen, upload->fp);
}
@@ -442,12 +462,12 @@
upload->size += wlen;
}
- if (upload->size > 0 && (req->ApacheUploadHook == NULL)) {
+ if (upload->size > 0 && (req->upload_hook == NULL)) {
fseek(upload->fp, 0, 0);
}
else {
upload->fp = NULL;
- }
+ }
}
}
1.4 +5 -4 mod_dtcl/apache_request.h
Index: apache_request.h
===================================================================
RCS file: /home/cvs/mod_dtcl/apache_request.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- apache_request.h 2001/03/13 12:00:44 1.3
+++ apache_request.h 2001/03/15 19:36:57 1.4
@@ -19,8 +19,9 @@
int parsed;
int post_max;
int disable_uploads;
- int (*ApacheUploadHook)(void *ptr, char *buf, int len);
- void *hookptr; /* data for UploadHook */
+ int (*upload_hook)(void *ptr, char *buf, int len, const ApacheUpload *upload);
+ void *hook_data;
+ char* temp_dir;
request_rec *r;
} ApacheRequest;
@@ -28,6 +29,7 @@
ApacheUpload *next;
char *filename;
char *name;
+ char *tempname;
table *info;
FILE *fp;
long size;
@@ -74,10 +76,9 @@
#define ApacheRequest_parse(req) \
(req->status = req->parsed ? req->status : ApacheRequest___parse(req))
-FILE *ApacheRequest_tmpfile(ApacheRequest *req);
+FILE *ApacheRequest_tmpfile(ApacheRequest *req, ApacheUpload *upload);
ApacheUpload *ApacheUpload_new(ApacheRequest *req);
ApacheUpload *ApacheUpload_find(ApacheUpload *upload, char *name);
-
#define ApacheRequest_upload(req) \
((req->parsed || (ApacheRequest_parse(req) == OK)) ? req->upload : NULL)
1.24 +24 -14 mod_dtcl/mod_dtcl.c
Index: mod_dtcl.c
===================================================================
RCS file: /home/cvs/mod_dtcl/mod_dtcl.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- mod_dtcl.c 2001/03/13 12:00:45 1.23
+++ mod_dtcl.c 2001/03/15 19:36:58 1.24
@@ -58,7 +58,7 @@
* University of Illinois, Urbana-Champaign.
*/
-/* $Id: mod_dtcl.c,v 1.23 2001/03/13 12:00:45 davidw Exp $ */
+/* $Id: mod_dtcl.c,v 1.24 2001/03/15 19:36:58 davidw Exp $ */
/* mod_dtcl.c by David Welton <da...@apache.org> - originally mod_include. */
/* See http://tcl.apache.org/mod_dtcl/credits.ttml for additional credits. */
@@ -326,14 +326,24 @@
}
/* Function to be used should we desire to upload files to a variable */
-int dtcl_upload_hook(void *ptr, char *buf, int len)
+int dtcl_upload_hook(void *ptr, char *buf, int len, const ApacheUpload *upload)
{
Tcl_Interp *interp = ptr;
+ static void *oldptr;
+ int flag = 0;
+
+ /* if there is not a new file being added, keep adding it to the
+ same list element. Otherwise, start a new list element */
+ if (oldptr != upload)
+ flag = TCL_LIST_ELEMENT|TCL_APPEND_VALUE;
+ else
+ flag = TCL_APPEND_VALUE;
+
Tcl_ObjSetVar2(interp,
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("data", -1),
Tcl_NewByteArrayObj(buf, len),
- TCL_APPEND_VALUE);
+ flag);
return len;
}
@@ -705,8 +715,8 @@
req = ApacheRequest_new(r);
if (upload_files_to_var)
{
- req->hookptr = interp;
- req->ApacheUploadHook = dtcl_upload_hook;
+ req->hook_data = interp;
+ req->upload_hook = dtcl_upload_hook;
}
ApacheRequest___parse(req);
@@ -732,9 +742,9 @@
}
upload = req->upload;
-/* while (upload) */
- if (upload)
- {
+ /* Loop through uploaded files */
+ while (upload)
+ {
char *type = NULL;
char *channelname = NULL;
Tcl_Channel chan;
@@ -744,19 +754,19 @@
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("filename", -1),
Tcl_NewStringObj(upload->filename, -1),
- 0);
+ TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
/* The variable name of the file upload */
Tcl_ObjSetVar2(interp,
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("name", -1),
Tcl_NewStringObj(upload->name, -1),
- 0);
+ TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
Tcl_ObjSetVar2(interp,
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("size", -1),
Tcl_NewIntObj(upload->size),
- 0);
+ TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
type = (char *)ap_table_get(upload->info, "Content-type");
if (type)
{
@@ -764,7 +774,7 @@
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("type", -1),
Tcl_NewStringObj(type, -1), /* kill end of line */
- 0);
+ TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
}
if (!upload_files_to_var)
{
@@ -775,10 +785,10 @@
Tcl_NewStringObj("::request::UPLOAD", -1),
Tcl_NewStringObj("channelname", -1),
Tcl_NewStringObj(channelname, -1), /* kill end of line */
- 0);
+ TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
}
- /* upload = upload->next; */
+ upload = upload->next;
}
if(!strcmp(r->content_type, "application/x-httpd-tcl"))