[Coldstuff] suspending tasks

Jeremy S Lowery coldstuff@cold.org
Wed, 24 Jul 2002 13:56:06 -0500


Message: 9
To: coldstuff@cold.org
From: michael@mudge.com
Subject: Re: [Coldstuff] suspending tasks
Date: Wed, 24 Jul 2002 09:38:35 -0700 (PDT)
Reply-To: coldstuff@cold.org

On Wed, 24 July 2002, Bruce Mitchener wrote

> Don't use cwritef() or whatever it was that dumps the file to the
> connection in one shot.  Open the file, read from it, write the data
to
> the network, and loop on that with a pause() in there.  You'll have to
> come up with what you think is a good chunksize to write each
iteration
> through the loop.

It's reading the file that's slow, not writing to the connection.

(Or, does sending a paragraph to a 300-baud modem cause immense
systemwide
lag!?)

This is all very core-specific, but your code might look something like
this:

blocksize = 8192;
file = $file_system.fopen(filename);
while(!file.feof()) {
  pause();
  connection.cwrite(file.fread(blocksize));
}

Of course, it would be very nice to have a pause-implied fread() and
cwritef(), along with the horrible $network.hostname().

- Kipp

Thanks a bunch Bruce, Brandon, and Kipp.

I've implemented it with a blocksize of 8192 and it seems to be working
fine. 

One other thing I'm planning on doing is implementing
multipart/form-data file uploads. I started working with cold-web and
have heavily modified it. One of the things that I think might cause a
problem is how the default system handles POST requests.

    len = (| request_headers['CONTENT_LENGTH] |);
    if ((len == ~keynf) || (!len))
          return .die(400, "No Content-Length");
    
    len = toint(len);
    
    // what a nightmare: sometimes the input for post will
    // not be received immediately, so we wait for it.
    //  this won't do...
    if (buflen(buffer) < len) {
        reading = 300;
        while ((buflen(buffer) < len) && --reading) {
            $sys.sleep();
            refresh();
        }
        if (buflen(buffer) < len) {
            buffer = `[];
            return .die(400, "Timeout on receiving POST request.");
        }
    }


Would this implementation cause problems with slow connections posting
larger files? Of course, the main reason I'm asking is because I don't
fully understand the $sys.sleep() and refresh() calls on this. 

Last thing is the buffer/string issue with the posted data. 

// RFC1867 file uploads support. 
    if(request_headers['CONTENT_TYPE] 
       == "application/x-www-form-urlencoded") {
    	// standard post data
    	body = buf_to_strings(body);
    	if ((body[listlen(body)]) == `[])
        	body = delete(body, listlen(body));
        else
        	body = replace(body, listlen(body),      
                       buf_to_str(body[listlen(body)]));
        for part in (body)
        	request_body = request_body.union(part.break_fields());
    } else
if(request_headers['CONTENT_TYPE].match_begin("multipart/form-data;")) {
    	// do the file upload thing.
    	// request_body will look something like
    	// #[["txtFirstName", "Jeremy"],
    	//   ["filePicture", #[['FILE, "C:\yahoo.gif"],
    	//                     ['CONTENT_TYPE, "image/gif"],
    	//			   ['DATA, `[<binary image data stored
as buffer>]]]
    	return .die(501, "RFC1867 File Uploads Not Supported");
    }

Would it be necessary to break off pieces of the buffer and convert them
to strings to do the matching for beginning and ends of the parts?
Unless I'm missing something, there's going to have to be a lot of
string/buffer hackery to get this implemented properly.

Thanks again,
JS Lowery