[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: 501 -- Almost there...! (plus a few questions)



Sender: Scott Klement <sk@xxxxxxxxxxxxxxxx>


Hi Dave,

> >From the examples I had seen I had the impression that if you looked at the
> HTML source of the page, found the fieldname in that source, you could say
> fieldname=filename and tack on a CRLF.  The HTML source is listed at the
> bottom.  The box for the file name appears to be "File".  The upload button
> that I want is immediately before the box.

The following is an excerpt from the HTML you that you sent me earlier,
reformatted slightly to make it easier to read:

<FORM ENCTYPE="multipart/form-data" ACTION="/cgi-bin/SomeScript.cgi" METHOD=POST>
<TD ALIGN=LEFT>
<INPUT TYPE=SUBMIT VALUE="Upload File">
</TD><TD ALIGN=LEFT>
<INPUT TYPE=FILE NAME="File" SIZE=20>
</TD>
</FORM>

<FORM METHOD="POST" ACTION="/cgi-bin/AnotherScript.cgi" ENCTYPE="application/x-www-form-urlencoded">
<TD ALIGN=LEFT>
<INPUT TYPE="submit" VALUE="Set Ascii" NAME="switch"></TD><TD ALIGN=LEFT>
<INPUT TYPE="submit" VALUE="Chg Passwd" NAME="switch">
</TD>
</FORM>

As you might be able to see, there are two distinct forms here.  Which one
gets submitted depends on which button you click.  If you click a button
in the first form (the "Upload File" button) it will send the file that's
stored in the box to the server referenced by the ACTION="" attribute of
the FORM.  The data that's sent will be encoded using the
"multipart/form-data" (MIME) scheme.  It'll be sent to SomeScript.cgi on
the same server as the HTML page was downloaded from.

If you click a button in the second form, it would instead use the
application/x-www-form-urlencoded encoding scheme and send either the
value "switch=Set+Ascii" or "switch=Chg+Passwd" to AnotherScript.cgi --
also on the same server as the HTML page was downloaded from.

The second form is very similar in concept to what's demonstrated in
EXAMPLE2 of HTTPAPI.  It encodes variables using the "url encoded"
encoding scheme.  This is a very common way of doing things on web sites.
In fact, I've already added a number of new routines to HTTPAPI 1.10
(which isn't yet available for download) that help with encoding these
variables.

However, the first form with the file upload box is somewhat more
difficult -- especially with HTTPAPI 1.9!

The reason it's difficult is that you have to encode your data using the
multipart/form-data scheme.  Since HTTPAPI 1.9 only allows POSTing data
that's on a pointer, you'll have to create a user space and encode your
file into that space.  Then, because HTTPAPI 1.9 translates POST data from
EBCDIC to ASCII, you have to make sure your data is in EBCDIC in the user
space.

What it actually sends to the web server is data that's got MIME
boundaries.  You tell the web server what those boundaries are in the
"content-type" field, which is the 7th parameter to the HTTP_url_post()
API call.   The data for that parameter should look like the following:

 'multipart/form-data; boundary=TestBoundaryBlahWhatever'

Be careful, as the content-type parameter is limited to to 64 bytes.  Also
string "TestBoundaryBlahWhatever" should be something that will never
appear inside the document itself.

Once you've figured out the content-type field, now you have to create the
user space and insert these MIME boundaries into the space.  If you want
to wait for HTTPAPI 1.10 to be released, then you can do it in a stream
file instead of using a user space.

The data with the boundaries added should look like this:

--TestBoundaryBlahWhatever
Content-Disposition: form-data; name="File"; filename="example.txt"
Content-Type: text/plain

<actual file data goes here>
--TestBoundaryBlahWhatever--

Note that the "TestBoundaryBlahWhatever" needs to match what you specified
for the content-type, except that there are two dashes "--" added to the front
of the opening boundary, and two dashes added to both the start & end of
the closing boundary.

Each of the lines of data shown above should end with a CRLF sequence.

The filename= should match the name of the file that you're uploading.

The content-type field should be the correct MIME type for the data that
you're uploading.  In the example above, the file is a plain text file,
thus "text/plain".  If, for example, the data is a JPG picture, it should
be specified as "image/jpeg".  Etc, etc...

All of the data that you store in the user space needs to be in EBCDIC as
HTTPAPI will translate it from ASCII to EBCDIC.  So even if the data is
NOT text, you have to translate it to EBCDIC when you load it into the
user space so that HTTPAPI will translate it back to the original byte
values when it does it's EBCDIC->ASCII translation.   I know that this is
a headache, so for this reason, the stream file upload in HTTPAPI 1.10
won't do this translation.

Once you've got all of this data laid out in your user space, you can
POST the data in the user space using the same method as EXAMPLE8 shows.
>
> Question #1:  How do I "click" the correct button?
>
> The file name that I want to upload is in the IFS.  When doing this from a
> browser I specify a path/filename, click the button, and away it goes.  I
> was hoping that the same with the IFS from my RPG.

This would be the case if I added a routine to HTTPAPI that specifically
does this -- but this issue has never come up before, so I've never done
that.

> Question #2:  How do I refer to this file name so that it can be
> uploaded when "the button is clicked"?
>
> When I tried assigning File=/IFSDIR/IFSFILE and posting it I got the
> following back:
> HTTP/1.1 501 Method Not Implemented

The web browser does extra work behind the scenes.  It doesn't only submit
the file's name, but actually encodes and sends the whole file as I
described above.

> Question #3:  What does this mean?

I have a feeling that you didn't encode the File= statement.  It treated
the spaces in the data as a separate method and that left it confused.
Really this question is moot since you don't need to send a file= string
to the server.

> Question #4:  Am I nuts?

Heh... I'll let someone else answer that question :)

At any rate, I'm sorry it took so long to get back to you.  I was out of
town from thursday - monday, and although I had some limited e-mail
access, I didn't have time for a message this long and detailed.  I'm glad
Brad Stone was able to help you out with some of the issues.

I was actually considering writing up some sample code to demonstrate the
process that I described above, but so far I havent' been able to make
time to do that.

---
Scott Klement  http://www.scottklement.com

-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubsribe from the list send mail
to majordomo@xxxxxxxxxxxxx with the body: unsubscribe ftpapi mymailaddr
-----------------------------------------------------------------------