How to stream file uploads with the Racket web server?

51 views
Skip to first unread message

Brian Adkins

unread,
Feb 13, 2020, 10:53:55 AM2/13/20
to Racket Users
I'm posting a file to my web app using the following form:

<form accept-charset="UTF-8" action="@(url-for 'upload-contacts (organization-id org-obj))"
      method="post" enctype="multipart/form-data" class="file-upload-form">
...
</form>

I use a simple function to create a hashtable of attributes:

(define (form-values req)
  (for/hash ([ b (in-list (request-bindings/raw req)) ])
    (cond [ (binding:form? b) (values
                               (bytes->string/utf-8 (binding-id b) #\space)
                               (bytes->string/utf-8 (binding:form-value b) #\space)) ]
          [ (binding:file? b) (values
                               (bytes->string/utf-8 (binding-id b) #\space)
                               (binding:file-content b)) ])))

It appears that the entire file contents are in the binding by the time the request is available to me. This is fine for "small enough" files, but for larger files, it would be great to be able to stream the file contents. The solution may be to use something like nginx's upload module:


But before I go down that route, I thought I'd ask if the Racket web server provides a more direct way to accomplish this.

Thanks,
Brian

Bogdan Popa

unread,
Feb 13, 2020, 11:02:33 AM2/13/20
to Brian Adkins, Racket Users
The version of the web-server that will be included with Racket 7.6
changes the way file uploads are handled so that they get offloaded to
disk after a certain threshold (similar to how that nginx module you
linked to works).

You can check out the pre-release docs for details:

* https://pre-release.racket-lang.org/doc/web-server/http.html?q=binding%3Afile#%28def._%28%28lib._web-server%2Fhttp%2Frequest-structs..rkt%29._make-binding~3afile%2Fport%29%29
* https://pre-release.racket-lang.org/doc/web-server-internal/dispatch-server-unit.html#%28part._safety-limits%29

To get these changes ahead of the release, you should be able to install
an updated version of `web-server-lib' from the package server or from git.

Hope that helps!

- Bogdan

Brian Adkins writes:

> I'm posting a file to my web app using the following form:
>
> <form accept-charset="UTF-8" action="@(url-for 'upload-contacts
> (organization-id org-obj))"
> method="post" *enctype="multipart/form-data"*
Message has been deleted

Brian Adkins

unread,
Feb 13, 2020, 3:57:26 PM2/13/20
to Racket Users
I tried replying earlier today, but somehow the post got deleted - could've been user error I suppose.

Anyway, the gist of the response was how I continue to be amazed by how often I get pleasant surprises with Racket - either there is some facility to do what I want that I just haven't found yet, or in this case where there soon will be :)

I have a couple Racket web apps in production now, so I think I have enough real world examples to begin extracting code into a web framework. Although it will look significantly different than Rails (more functional, less object oriented), I'm hoping to achieve similar ease-of-use functionality. I first viewed the Rails "weblog in 15 minutes" video fourteen years ago, and it had a significant impact on my professional life (at the time I was working in Java w/ Spring & Hibernate, etc.). The video is dated now & has some annoying idiosyncrasies, but if you haven't seen it, and you're interested in web development (in any language), it's worth viewing just to get the overview of Rails ease of use:

Robby Findler

unread,
Feb 14, 2020, 7:36:26 AM2/14/20
to Brian Adkins, Racket Users
It is possible that your post wasn't deleted but got held up in
google's spam traps. I just approved a message from you (that I was
alerted to only this morning). Was that the message?

Robby
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6a3298ff-a40c-4a6e-9f16-b1c95144e482%40googlegroups.com.

Brian Adkins

unread,
Feb 14, 2020, 8:42:24 AM2/14/20
to Racket Users
Hmm... I don't see a new message in the thread, so I'm not sure what you approved. No worries though.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages