Igor Korot | 13 Jul 2012 01:49
Picon

Is it possible to simply write to a socket?

Hi, ALL,
I'm transferring the code from VB to C/C++ to make it cross-platform.
In VB, there is following code:

[code]
Dim boundary As String = "---------------------------" &
DateTime.Now.Ticks.ToString("x")
Dim newLine As String = System.Environment.NewLine
Dim boundaryBytes As Byte() =
System.Text.Encoding.ASCII.GetBytes(newLine & "--" & boundary &
newLine)
Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)

request.ContentType = "multipart/form-data; boundary=" & boundary
request.Method = "POST"
request.KeepAlive = True
request.Credentials = Net.CredentialCache.DefaultCredentials

Using requestStream As IO.Stream = request.GetRequestStream()
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
End Using
[/code]

AFAIU, it just simply writes boundary bytes to the socket as an ASCII
bytes sequence.
There is post-ing involved, no form submittal
Is it possible to do the same with libcurl?

Because with the following approach:

(Continue reading)

Guenter | 13 Jul 2012 04:41

Re: Is it possible to simply write to a socket?

Hi Igor,
Am 13.07.2012 01:49, schrieb Igor Korot:
> I'm transferring the code from VB to C/C++ to make it cross-platform.
> In VB, there is following code:
>
> [code]
> Dim boundary As String = "---------------------------"&
> DateTime.Now.Ticks.ToString("x")
> Dim newLine As String = System.Environment.NewLine
> Dim boundaryBytes As Byte() =
> System.Text.Encoding.ASCII.GetBytes(newLine&  "--"&  boundary&
> newLine)
> Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
>
> request.ContentType = "multipart/form-data; boundary="&  boundary
> request.Method = "POST"
> request.KeepAlive = True
> request.Credentials = Net.CredentialCache.DefaultCredentials
>
> Using requestStream As IO.Stream = request.GetRequestStream()
>              requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
> End Using
> [/code]
>
> AFAIU, it just simply writes boundary bytes to the socket as an ASCII
> bytes sequence.
> There is post-ing involved, no form submittal
> Is it possible to do the same with libcurl?
although this might be possible with libcurl - if I were you I would use 
Perl for this task; you have then a simple script which runs on almost 
(Continue reading)

Igor Korot | 13 Jul 2012 07:01
Picon

Re: Is it possible to simply write to a socket?

Guenter,

On Thu, Jul 12, 2012 at 7:41 PM, Guenter <lists <at> gknw.net> wrote:
> Hi Igor,
> Am 13.07.2012 01:49, schrieb Igor Korot:
>
>> I'm transferring the code from VB to C/C++ to make it cross-platform.
>> In VB, there is following code:
>>
>> [code]
>> Dim boundary As String = "---------------------------"&
>> DateTime.Now.Ticks.ToString("x")
>> Dim newLine As String = System.Environment.NewLine
>> Dim boundaryBytes As Byte() =
>> System.Text.Encoding.ASCII.GetBytes(newLine&  "--"&  boundary&
>> newLine)
>> Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
>>
>> request.ContentType = "multipart/form-data; boundary="&  boundary
>> request.Method = "POST"
>> request.KeepAlive = True
>> request.Credentials = Net.CredentialCache.DefaultCredentials
>>
>> Using requestStream As IO.Stream = request.GetRequestStream()
>>              requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
>> End Using
>> [/code]
>>
>> AFAIU, it just simply writes boundary bytes to the socket as an ASCII
>> bytes sequence.
(Continue reading)

Oscar Koeroo | 13 Jul 2012 07:33
Picon
Picon
Favicon

Re: Is it possible to simply write to a socket?

Hi Igor,

Your approach breaks the fact that POST is typically a form-post. I recently
had a problem with that breakage when I tried to POST data in JSON as raw
data. libcurl can do this, but consider the incompatibility with something
like Django.

Here's an example in C:

[code]
static size_t
_curl_memread (void *ptr, size_t size, size_t nmemb, void *userp) {
    size_t read_size = 0;
    if (!userp) {
        return 0;
    }
    read_size = evbuffer_copyout((struct evbuffer *)userp, ptr, size * nmemb);
    return read_size;
}

    /* POST */
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS,
        (char *)evpull(hc->out));
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE,
        (long)evbuffer_get_length(hc->out));
    curl_easy_setopt(curl_handle, CURLOPT_URL, hc->url_query);
    curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, _curl_memread);
    curl_easy_setopt(curl_handle, CURLOPT_READDATA, (void *)hc->out);

    curl_easy_perform(curl_handle);
(Continue reading)

Igor Korot | 13 Jul 2012 09:05
Picon

Re: Is it possible to simply write to a socket?

Hi, Oscar,

On Thu, Jul 12, 2012 at 10:33 PM, Oscar Koeroo <okoeroo <at> nikhef.nl> wrote:
> Hi Igor,
>
> Your approach breaks the fact that POST is typically a form-post. I recently
> had a problem with that breakage when I tried to POST data in JSON as raw
> data. libcurl can do this, but consider the incompatibility with something
> like Django.

The server code is written in ASP.
The code has a form behind.

The approach for the ASP code is as follows:
1. Send the boundary string.
2. Now post some arbitrary data about the file we are posting (like
the name and id).
3. Tell server we are posting the file in the image/bitmap format.
4. Send the file by chunks
5. Send the same boundary string again to indicate the transfer is done.

I didn't write the server code and I see only VB client code that I
need to translate.
Now in VB steps 2 and 3 are done in one write() call.

It is a multiform POST as the file is read and send out by chunks.
And so I have to follow a protocol to communicate with the server.

Any help on libcurl implementation?

(Continue reading)

Oscar Koeroo | 13 Jul 2012 11:06
Picon
Picon
Favicon

Re: Is it possible to simply write to a socket?

Hi Igor,

My code snippet shows that I have my to-be posted data in a buffer. I
present libcurl with the length of it and when the code hits the
curl_easy_perform() it will POST the data. Libcurl will take automagically
take care of the splitting the data over mulitple HTTP messages as a
multi-part POST.

In my code snippet the hc->out is the output buffer to-be POST-ed.

Currently I might have made a code mistake myself by setting POST fields
data and the READ stuff. But bare with me that this is just an example. :-)

In your case I can imagine that you need to send a few HTTP POST's to get
the complete job done. This is all doable and there are example on how to
make this even more nice on the Curl website.

I think this information might be of assistance too:
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTHTTPPOST

	Oscar

On 13-07-12 09:05, Igor Korot wrote:
> Hi, Oscar,
> 
> On Thu, Jul 12, 2012 at 10:33 PM, Oscar Koeroo <okoeroo <at> nikhef.nl> wrote:
>> Hi Igor,
>>
>> Your approach breaks the fact that POST is typically a form-post. I recently
>> had a problem with that breakage when I tried to POST data in JSON as raw
(Continue reading)


Gmane