h2o / h2o/picohttpparser

Not able to properly parse using ruby FFI

Open
#43 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
2.1k
Forks
274
PR merge metrics
No merged PRs in 30d

Description

Hi, I'm trying to create an FFI binding for ruby. Unfortunately, haven't been able to progress in the most basic example, as I can't get the verb and path strings back. Since I'm a bit out of ideas on how to further debug it, came asking for advice. I've contained it in a small-purpose script:

```ruby
# tested with ruby 2.5 and 24, ruby-ffi 1.9.25
require 'ffi'

module Ext
extend FFI::Library
ffi_lib './ext/x86_64-darwin/libpico-http-parser-ext.bundle'
attach_function :phr_parse_request, [:pointer, :size_t, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer, :size_t], :int
end

REQUEST = +"GET /test?ok=1 HTTP/1.1\r\nUser-Agent: curl/7.18.0\r\nHost: 0.0.0.0:5000\r\nAccept: */*\r\nContent-Length: 5\r\n\r\nWorld".b

verb = FFI::MemoryPointer.new(:pointer)
verb_len = FFI::MemoryPointer.new(:size_t)
path = FFI::MemoryPointer.new(:pointer)
path_len = FFI::MemoryPointer.new(:size_t)
minor_version = FFI::MemoryPointer.new(:int)
header_reader = FFI::MemoryPointer.new(:pointer)
header_reader_len = FFI::MemoryPointer.new(:int)
header_reader_len.write_int(128)

res = Ext.phr_parse_request(REQUEST, REQUEST.bytesize, verb, verb_len, path, path_len, minor_version, header_reader, header_reader_len, 0)

puts "bytes parsed: #{res}"
puts "method: #{verb.read_string(verb_len.read_int).inspect}"
puts "path: #{path.read_string(path_len.read_int).inspect}"
puts "version: HTTP/1.#{minor_version.read_int.inspect}"

# bytes parsed: 104
# method: "0d\x98"
# in `get_bytes': Memory access offset=0 size=10 is out of bounds (IndexError)
```

@kazuho did you have some success using ffi in any other language? I've seen your perl parser and also @kazeburo 's ruby c-extension binding, but sadly neither could help get to the bottom of this. Some ruby-FFI-specific issue?

Contributor guide

No contributing guide indexed for this repository

Research direction

The reproduction is contained in the issue body and loads ./ext/x86_64-darwin/libpico-http-parser-ext.bundle; start by checking the Ruby FFI declarations and pointer arguments against phr_parse_request. Compare the returned pointers and lengths with the parser's expected outputs, and confirm the script can read the verb and path without an out-of-bounds error.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.