Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.networking > #2199

Re: HTTP POST for file upload

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Josef Moellers <josef.moellers@invalid.invalid>
Newsgroups comp.os.linux.networking
Subject Re: HTTP POST for file upload
Date Thu, 06 Jun 2013 21:26:55 +0200
Lines 58
Message-ID <b1c63vFedj3U1@mid.individual.net> (permalink)
References <b19bg3FpriuU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Trace individual.net UNT14A4a6G6gVm+jrsgq4wudVwtFnVfyim3KMiYvM4IAHkTYsf
Cancel-Lock sha1:oKylhZgr3yzLuhT9gci6U2dhjdA=
User-Agent Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130510 Thunderbird/17.0.6
In-Reply-To <b19bg3FpriuU1@mid.individual.net>
X-Enigmail-Version 1.5.1
Xref csiph.com comp.os.linux.networking:2199

Show key headers only | View raw


On 06/05/2013 07:40 PM, Josef Moellers wrote:
> Hi,
> 
> I'm trying to extend an application to upload result files to a web
> server using HTMP POST. By and large it works but the last couple of
> bytes (the last line, to be precise) does not show up in the received
> data. Maybe someone more knowing than I has an idea what I'm doing wrong.

I found the solution in RFC 1341
(<http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html>:

7.2.1 Multipart: The common syntax
:
Note that the encapsulation boundary must occur at the beginning of a
line, i.e., following a CRLF, and that that initial CRLF is considered
to be part of the encapsulation boundary rather than part of the
preceding part.
:

So I changed my code to

>     snprintf(boundary, sizeof(boundary),
> "WebKitFormBoundaryePkpFF7tjBAqx29L");
>     content_length = 0;
>     content_length += fprintf(tmpfp, "\r\n------%s\r\n", boundary);
>     content_length += fprintf(tmpfp, "Content-Disposition: form-data;
> name=\"archive_name\"\r\n");
>     content_length += fprintf(tmpfp, "\r\n");
>     content_length += fprintf(tmpfp, "%s", archive_name);
>     content_length += fprintf(tmpfp, "\r\n------%s\r\n", boundary);
>     content_length += fprintf(tmpfp, "Content-Disposition: form-data;
> name=\"destination_name\"\r\n");
>     content_length += fprintf(tmpfp, "\r\n");
>     content_length += fprintf(tmpfp, "%s", remote_name);
>     content_length += fprintf(tmpfp, "\r\n------%s\r\n", boundary);
>     content_length += fprintf(tmpfp, "Content-Disposition: form-data;
> name=\"file_contents\"; filename=\"%s\"\r\n", local_name);
>     content_length += fprintf(tmpfp, "Content-Type:
> application/x-object\r\n");
>     content_length += fprintf(tmpfp, "\r\n");
>     while ((nread = fread(buffer, 1, sizeof(buffer), src)) > 0)
>     {
>     	fwrite(buffer, 1, nread, tmpfp);
> 	content_length += nread;
>     }
>     content_length += fprintf(tmpfp, "\r\n------%s--\r\n", boundary);

Apparently the Perl CGI code was tolerant and took the "}\n" as the CRLF
preceding the last boundary and dropped that!

Another problem, not related to this one, was that I had to explicitly
close the write side of the socket in order to reliably receive the
response.

Thanks for both who helped (and all who thought about the issue).

Josef

Back to comp.os.linux.networking | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

HTTP POST for file upload Josef Moellers <josef.moellers@invalid.invalid> - 2013-06-05 19:40 +0200
  Re: HTTP POST for file upload Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2013-06-05 19:51 +0200
  Re: HTTP POST for file upload Richard Kettlewell <rjk@greenend.org.uk> - 2013-06-05 18:53 +0100
  Re: HTTP POST for file upload Josef Moellers <josef.moellers@invalid.invalid> - 2013-06-06 21:26 +0200
    Re: HTTP POST for file upload Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-06-06 21:51 +0000

csiph-web