Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 12:23
    straight-shoota assigned #12740
  • 12:23
    straight-shoota synchronize #12740
  • 11:28
    straight-shoota closed #11409
  • 11:28
    straight-shoota labeled #11409
  • 11:28
    straight-shoota labeled #11409
  • 11:25
    straight-shoota labeled #12829
  • 11:25
    straight-shoota labeled #12829
  • 11:25
    straight-shoota closed #7532
  • 11:24
    straight-shoota labeled #7532
  • 11:24
    straight-shoota labeled #7532
  • 11:24
    straight-shoota opened #12829
  • 11:12
    straight-shoota milestoned #10531
  • 11:12
    straight-shoota milestoned #10531
  • 08:23
    caspiano edited #10531
  • Dec 08 23:04
    HertzDevil edited #12623
  • Dec 08 22:57
    Blacksmoke16 edited #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:54
    HertzDevil labeled #12828
  • Dec 08 22:54
    HertzDevil labeled #12828
From IRC (bridge bot)
@FromIRC
<oprypin> by the way if you read a string from a http endpoint producing arbitrary bytes, that will NOT be fine
<oprypin> probably, i dunno. really unreliable in any case
David Blurton
@davidblurton
Right, doesn't make any sense.
I'll stick to slices
Can just read a byte at a time and see if it's utf8
From IRC (bridge bot)
@FromIRC
<oprypin> @davidblurton, ...uh no?
<oprypin> it has to be known in advance, defined by a protocol, guessing never works
David Blurton
@davidblurton
I won't be guessing. But I do need to parse the data. Some of it will be strings, some of it bytes.
From IRC (bridge bot)
@FromIRC
<oprypin> davidblurton so you have an IO and call this when you expect utf-8 encoded text https://crystal-lang.org/api/0.23.1/IO.html#read_string%28bytesize%3AInt%29%3AString-instance-method and other methods otherwise
David Blurton
@davidblurton
def get_raw_body(url) 
  HTTP::Client.get url do |response|
    length = response.headers.get("content_length")[0].to_i

    slice = Slice(UInt8).new(length)
    response.body_io.read_fully(slice)
    slice
  end
end
@oprypin How can I improve this?
From IRC (bridge bot)
@FromIRC
<oprypin> i'm at a loss, can't see an improvement
David Blurton
@davidblurton
Just leave the data in the an Array? I feel like slices are nicer though
From IRC (bridge bot)
@FromIRC
<hightower2> Hey, isn't behavior of Dir.entries( "/directory") a bit weird given that it returns "." and ".." among the results, in unclear sort order? I called it and it returned ["three", "four", "one", ".", "two", ".."]
<oprypin> but maybe you don't actually want to turn it into a slice and leave it in the IO
David Blurton
@davidblurton
That's true, I could pass the IO directly to my body parser
From IRC (bridge bot)
@FromIRC
<oprypin> hightower2, the sort order is not weird, but the .. stuff is indeed silly; unusable
<oprypin> so with the IO you can read a few bytes to a slice, then read a few bytes as a string
<oprypin> which seems to be what you want
<oprypin> but it's always weird how inconvenient it is to go from IO to slice. i guess they don't want people to be wasteful with allocations
<oprypin> like, instead of making a loop creating a new slice every time this encourages reuse
David Blurton
@davidblurton
I guess I was expecting the client response to have a raw_body property or something
From IRC (bridge bot)
@FromIRC
<oprypin> @davidblurton, but it allows streaming, it does not contain the whole response
<oprypin> abstracting streaming is the whole reason for IO to exist in addition to Slice
David Blurton
@davidblurton
right, I'm looking for a simple way to read the whole body though
I can see that IO is a nice abstraction for reading and writing, but I don't really care about streams here
From IRC (bridge bot)
@FromIRC
<oprypin> @davidblurton, but it's also a nice abstraction for protocol parsing
David Blurton
@davidblurton
I agree, I was using Char::Reader before, until I realised some of the response was binary
so should be easy to swap that for an IO
John Jansen
@johnjansen
anyone got a nice way to timeout an http get
ive done it before, now i cant find the code, or remember what i did
forget i asked … stupid me ;-/
From IRC (bridge bot)
@FromIRC
<RX14> @johnjansen you need to use HTTP::Client.new then set the timeout on that client instance
<RX14> HTTP::Client needs some love to be honest
John Jansen
@johnjansen
yep … didnt think to look first, did i ;-)
From IRC (bridge bot)
@FromIRC
<hightower2> Hey, if I need to write a 16bit unsigned int in little endian order into some buffer, then 1) What buffer do I use? StringBuilder? and 2) How do I write something like that?
John Jansen
@johnjansen
@RX14 might have an answer to that, he was here a little while ago
From IRC (bridge bot)
@FromIRC
<RX14> hightower2, you should look at the write_bytes method on IO
<RX14> String.build yields you an IO instance
<RX14> so you can use that
<RX14> however writing a raw integer to a string is unlikely to be what you want
<RX14> strings shouldn't contain binary data
<RX14> if you're working with binary data, IO::Memory and Bytes should be what you use
<hightower2> @RX14, right, was just about to say that, thanks
Chris Watson
@watzon
Anyone know where stddef.h would come from? I seem to be missing it. Reinstalled clang and gcc already
Never mind, I found it in /usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/include, but clang seems to not be picking it up for some reason. I'm trying to use crystal_lib and it keeps telling me /usr/include/sys/types.h:145:10: fatal error: 'stddef.h' file not found
From IRC (bridge bot)
@FromIRC
<hightower2> I am trying to write data to a socket, and invoke some method/callback when the data is actually written out. Is there a built-in way for this?
Doug
@DougEverly
@drosehn see if https://github.com/DougEverly/daemonize.cr works for you
Chris Watson
@watzon
What would the Crystal equivilent of unsigned long be?
Would I do $var_name : Float64?