Skip to content

r.binary article #467

Closed
Closed

Description

@chipotle

Write an article with a couple examples for using r.binary.

Activity

  1. self-assigned this
    on Aug 18, 2014
  2. added this to the 1.14 milestone on Aug 18, 2014
  3. coffeemug commented on Aug 18, 2014

    @coffeemug
    Contributor

    Note, it would be very cool if we had two examples:

    • How to read a small file and insert it as a field in a document
    • How to insert a file into a document via r.http (e.g. a gravatar icon)
  4. neumino commented on Aug 18, 2014

    @neumino
    Member

    r.binary(r.http(...)) doesn't actually work.
    r.binary only accepts a Buffer (or similar things in Python/Ruby).

  5. chipotle commented on Aug 18, 2014

    @chipotle
    ContributorAuthor

    If you use r.http('http://www.gravatar.com/avatar/18b515cae379d4333296db7121b80458', result_format='binary'), that's supposed to return a binary object directly, right? So you wouldn't need the r.binary() wrapper around it -- you'd just use the binary result_format argument. Or is this incorrect?

  6. coffeemug commented on Aug 18, 2014

    @coffeemug
    Contributor

    I was under the impression that @Tryneus fixed the content type issues in r.http and you can insert binary data via r.http into documents. It's possible that r.binary(r.http) doesn't work, but I think there is a different way of doing it.

    @Tryneus -- could you comment on this? Should we make r.binary(r.http) work explicitly? (I think we should)

  7. neumino commented on Aug 18, 2014

    @neumino
    Member

    Yep, like @chipotle said, you basically just do

    r.http(<url>, {resultFormat: "binary"})
    

    And you may skip the resultFormat optarg in most of the cases.

    r.binary is just a client term that convert a buffer to an object like {$reql_type$: "BINARY", data: <base64string>} and will not accept anything else than a buffer.

  8. coffeemug commented on Aug 18, 2014

    @coffeemug
    Contributor

    I think we should allow r.binary(r.http()) to avoid confusing users. Let's wait until @Tryneus chimes in.

  9. Tryneus commented on Aug 18, 2014

    @Tryneus
    Member

    Opened rethinkdb/rethinkdb#2931 for adding r.binary on the server. For the most part it's just another way of writing coerce_to('binary').

  10. Tryneus commented on Aug 19, 2014

    @Tryneus
    Member

    r.binary(r.http()) will now work. Keep in mind that if the HTTP response is interpreted as a string but contains NUL characters, it will still error. The only way around that is to use result_format=binary.

  11. mlucy commented on Aug 19, 2014

    @mlucy
    Member

    We should try very hard not to encourage people ro write r.binary(r.http()). It will appear to work until there's a NULL character, or a future version of RethinkDB actually checks for UTF-8, but then it will just blow up.

  12. deontologician commented on Aug 19, 2014

    @deontologician
    Contributor

    Can't you just rewrite the term on the server from r.binary(r.http()) to
    r.http(..., result_format='binary') ? Seems like that's the abstraction
    people would be hoping for when wrapping it like that

    On Mon, Aug 18, 2014 at 7:00 PM, Michael Lucy [email protected]
    wrote:

    We should try very hard not to encourage people ro write
    r.binary(r.http()). It will appear to work until there's a NULL
    character, or a future version of RethinkDB actually checks for UTF-8, but
    then it will just blow up.

    —
    Reply to this email directly or view it on GitHub
    #467 (comment).

  13. Tryneus commented on Aug 19, 2014

    @Tryneus
    Member

    @deontologician, I'm not aware of any way we can do look-ahead like that in the query-language. @mlucy, any ideas? Would we perhaps be able to use eval_flags_t for this?

  14. mlucy commented on Aug 19, 2014

    @mlucy
    Member

    @deontologician: if you mean "is it technically feasible", then the answer is yes, but it would be bad because that's not how our type system works in general. We'd have to hard-code that combination, and it would be extremely confusing because r.binary(r.http()) would work but e.g. r.binary("PREFIX" + r.http()) still wouldn't. (In general, having the return type of a term depend on the term calling it seems confusing to me.)

  15. mlucy commented on Aug 19, 2014

    @mlucy
    Member

    @Tryneus: If we did want to do this, we'd probably do it at compile-time in binary_term_ts constructor (we'd look at the source code for its arguments and add the binary optarg to http calls). eval_flags_t is evil and needs to die.

  16. 4 remaining items

  17. Tryneus commented on Aug 19, 2014

    @Tryneus
    Member

    Well, if you're using result_format='auto', your return type could randomly change in any situation, and at least wrapping it in r.binary would force it to be consistent or error. Not that I think it's a perfectly good solution, but it might be better than the current situation.

  18. mlucy commented on Aug 19, 2014

    @mlucy
    Member

    @Tryneus: how do we currently detect the result format when it's set to auto? Detecting based on the content-type seems very different from detecting based on the presence or absence of a null byte.

    @deontologician: I don't quite follow. If the content-type says it's binary data, shouldn't we just return binary data? Do we not currently do that?

  19. Tryneus commented on Aug 19, 2014

    @Tryneus
    Member

    The r.http command currently uses the Content-Type of the response if and only if result_format='auto':

    • application/json - Parse as JSON
    • application/json-p, text/javascript, text/json-p - Try a few things in order:
      • Attempt to parse as JSON
      • Attempt to parse as JSONP
      • String
    • audio/*, image/*, video/*, application/octet-stream - Binary
    • Everything else - String
  20. deontologician commented on Aug 20, 2014

    @deontologician
    Contributor

    OK, so my confusion was when it would blow up. @mlucy explained that it
    wouldn't make it out of the r.http call if there was a nul, I was thinking
    it would make it out and would blow up somewhere else down the line, which
    would be avoided if the very next thing was converting it to a binary.

    What if r.http validates against utf-8 for real (rather than just nul
    checking) and then returns a binary if that fails? It's unlikely that a
    random binary file happens to be utf-8 valid (I would guess for all but the
    shortest strings, effectively impossible).

    On Tue, Aug 19, 2014 at 4:19 PM, Tryneus [email protected] wrote:

    The r.http command currently uses the Content-Type of the response if and
    only if result_format='auto':

    • application/json - Parse as JSON
    • application/json-p, text/javascript, text/json-p - Try a few things
      in order:
      • Attempt to parse as JSON
      • Attempt to parse as JSONP
      • String
        • audio/, image/, video/*, application/octet-stream - Binary
    • Everything else - String

    —
    Reply to this email directly or view it on GitHub
    #467 (comment).

  21. Tryneus commented on Aug 20, 2014

    @Tryneus
    Member

    Ideally we would validate all strings like that, but we currently don't and haven't ever done that, and I think it would be very unexpected behavior to only validate strings that arrive from r.http and nowhere else. We also can't change it anywhere else without potentially breaking users' applications, so that kind of a change would need to be in a new major version of the query language (e.g. 2.0).

  22. mlucy commented on Aug 20, 2014

    @mlucy
    Member

    If we have code to validate UTF-8 strings, we should probably just use it everywhere.

  23. deontologician commented on Aug 20, 2014

    @deontologician
    Contributor

    Well, actually, now that I think about it, it seems like this is a general
    issue with r.http. If a page doesn't get automatically detected as binary,
    and happens to contain a nul character, it's going to blow up anyway,
    regardless of whether the enclosing term is r.binary or not.

    As to string validation everywhere vs. just in r.http, I think it makes
    sense since outside data is dirty. There is always the possibility of some
    application/X-Foo content-type that runs the risk of having a nul
    character in it. In light of that, I'd say it makes sense to do heavier
    validation, or switch the default result type to be binary instead of string

  24. deontologician commented on Aug 20, 2014

    @deontologician
    Contributor

    to clarify: I think it's justifiable to only have the validation in
    r.http, since that's kind of like a gatekeeper of sorts pulling
    potentially dirty data into the database.

  25. Tryneus commented on Aug 20, 2014

    @Tryneus
    Member

    @deontologician, does that mean you support my suggestion to have r.http produce a binary object when result_format='auto' and creating a string fails? (Note: this should only happen for invalid JSON-P and unrecognized Content-Types from the above list)

    @mlucy, it doesn't seem that having result_format='auto' work in this way is much different than how we handle JSON-P.

  26. removed this from the 1.14 milestone on Aug 20, 2014
  27. chipotle commented on Aug 20, 2014

    @chipotle
    ContributorAuthor

    I'm taking this out of the 1.14 docs milestone because I don't think it should be a release blocker. (The docs for r.binary itself with examples are already there, and the article's written and in review. And doesn't use r.binary(r.http()) in it!)

  28. deontologician commented on Aug 20, 2014

    @deontologician
    Contributor

    @Tryneus yes, but I think creating the string should be subject to utf-8 validation, rather than just checking for nul bytes. It may be that this is a big performance hit, in which case it's probably safer to default to returning a binary unless we recognize the content-type as explicitly not binary.

  29. deontologician commented on Aug 20, 2014

    @deontologician
    Contributor

    Also @chipotle, sorry for hijacking this docs thread. It probably needs to be its own issue

  30. mlucy commented on Aug 20, 2014

    @mlucy
    Member

    This should probably be an issue in the main rethinkdb issue tracker tagged RQL_proposal. We can discuss it during the upcoming discussion period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions