[Ur] returnBlob behavior?
Sergey Mironov
grrwlf at gmail.com
Mon Feb 20 03:27:09 EST 2017
Ah, I figured it out. Yes, Adam is right, everything works as expected.
Regards,
Sergey
2017-02-20 11:22 GMT+03:00 Sergey Mironov <grrwlf at gmail.com>:
> Hi. I could confirm that the application posted by Marko returns file
> content in both branches of [if]. I agree that it is strange.
>
> Below is the code UrWeb generated on my machine. At the first glance,
> it is expected to return blob only on the size check failure. Somehow,
> returnBlob is executed in the both cases.
>
> Regards,
> Sergey
>
>
> static uw_unit
> __uwn_wrap_echoLength_4277(uw_context ctx, struct __uws_1 __uwr_x0_0,
> uw_unit __uwr___1)
> {
> return(({
> uw_unit __uwr___2 =
> (uw_begin_region(ctx), uw_Basis_debug(ctx,
> uw_Basis_intToString(ctx,
> uw_Basis_blobSize(ctx,
> uw_Basis_fileData(ctx,
> __uwr_x0_0.__uwf_File)))));
> uw_end_region(ctx);
> uw_Basis_htmlifyInt_w(ctx,
> uw_Basis_strlen(ctx,
> ({
> uw_Basis_bool disc =
> !
> (uw_Basis_blobSize(ctx,
> uw_Basis_fileData(ctx, __uwr_x0_0.__uwf_File)) <= 100000LL);
>
> disc == uw_Basis_True ? "Whoa! That one's too big."
> :
> disc == uw_Basis_False ?
> ({
> uw_Basis_blob blob =
> uw_Basis_fileData(ctx, __uwr_x0_0.__uwf_File);
> uw_Basis_string mimeType =
> uw_Basis_blessMime(ctx,
> uw_Basis_fileMimeType(ctx, __uwr_x0_0.__uwf_File));
> uw_Basis_string
> tmp;
> uw_return_blob(ctx, blob, mimeType);
> tmp;
> })
> :
> ({
> uw_Basis_string
> tmp;
> uw_error(ctx, FATAL,
> "/nix/store/fby0l6fdwqrq0rsr25jbc9hhg2srix08-urweb-urp-Urpf/Urpf.ur:11:32-11:33:
> pattern match failure");
> tmp;
> });
> })));
> }));
> }
>
> 2017-02-19 18:15 GMT+03:00 Adam Chlipala <adamc at csail.mit.edu>:
>> I think you've gotten the wrong idea about [returnBlob]. It operates in the
>> style of exceptions, ending the transaction with the given result. It's not
>> a normal function return, so you can't use it pass a blob from one part of
>> your program to another. Also, it definitely doesn't generate strings.
>>
>> Somewhat intentionally, Ur/Web currently does not provide a way to
>> introspect into a blob, without using the FFI. It would take some thought
>> on what the right API would be. In my applications that involve CSV import,
>> I have their content pasted into textboxes.
>>
>>
>> On 02/19/2017 10:11 AM, Marko Schütz Schmuck wrote:
>>>
>>> I'm confused about the use of returnBlob.
>>>
>>> I was looking through the existing urweb demos to see how I could
>>> access the data in a blob of mime type "text/plain" as a string.
>>> Eventually, I want to import into a table from a CSV file the user
>>> uploads.
>>>
>>> I hoped that returnBlob used in a transaction string could achieve
>>> this. So I modified demo/upload.ur to
>>>
>>> fun echo (r : {File : file}) : transaction string =
>>> if blobSize (fileData r.File) > 100000 then
>>> return "Whoa! That one's too big."
>>> else
>>> returnBlob (fileData r.File) (blessMime (fileMimeType r.File))
>>>
>>> fun echoLength (r : {File : file}) : transaction page =
>>> s <- echo r ;
>>> return <xml>{[String.length s]}</xml>
>>>
>>> fun main () : transaction page
>>> = return <xml><body>
>>> <h1>The Amazing File Echoer!</h1>
>>>
>>> <form>Upload a file: <upload{#File}/> <submit
>>> action={echoLength}/></form>
>>> </body></xml>
>>>
>>> This compiles and runs, and when I supply a file longer than 100000 I
>>> get the expected page with the result 26, but to my surprise a shorter
>>> file results in a page with the content of the file not it's length.
>>>
>>> I'd really appreciate some help.
>>>
>>> Thanks in advance and best regards,
>>>
>>> Marko
>>
>>
>> _______________________________________________
>> Ur mailing list
>> Ur at impredicative.com
>> http://www.impredicative.com/cgi-bin/mailman/listinfo/ur
More information about the Ur
mailing list