daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
779 stars 80 forks source link

Question #153

Closed otgo closed 4 years ago

otgo commented 5 years ago

How do I do this in lua-http?

-- I added here the libraries that I've used just in case.
https = require 'ssl.https'
ltn = require 'ltn12' 
https.request {
        ['url'] = self.URL..method,
        ['method'] = 'POST',
        ['headers'] = {
            ['Content-Type'] = ('%s; boundary=%s'):format('multipart/form-data', boundary),
            ['Content-Length'] = #source[1]
        },
        ['source'] = ltn12.source.string(source[1]),
        ['sink'] = ltn12.sink.table(response)
    }
daurnimator commented 5 years ago

You can just swap https = require 'ssl.https' for https = require "http.compat.socket". See https://daurnimator.github.io/lua-http/0.3/#http.compat.socket Otherwise I'm going to need a lot more context.

otgo commented 5 years ago

I did recently a test, all it's okay now, it is working good, but returns this: Segmentation Fault (core dumped) Like a C error...

Here my debug code:

local https = require 'http.compat.socket'
--https = require 'ssl.https'
local ltn12 = require 'ltn12'
function tprintf(_table_, _index_, _string_, ...)
    if not _table_[_index_] then
        _table_[_index_] = ''
    end
    _table_[_index_] = _table_[_index_] .. string.format(_string_, ...)
end
local source = {}
local response = {}
local index = 1
local boundary = 'BOUNDARY--mdd4s4Sd457dOp--END'
tprintf(source, index, '--%s\r\n', boundary)
tprintf(source, index, 'Content-Disposition: form-data; name="%s"\r\n\r\n', 'text')
tprintf(source, index, '%s\r\n', 'This is a test; sendMessage();')
tprintf(source, index, '--%s\r\n', boundary)
tprintf(source, index, 'Content-Disposition: form-data; name="%s"\r\n\r\n', 'chat_id')
tprintf(source, index, '%s\r\n', "189041244")
tprintf(source, index, '--%s--\r\n', boundary)
print(boundary)
https.request {
    ['url'] = 'https://api.telegram.org/bot213210238:AAEP1XgZOXjIiAqDJvUEDBzBHmLqAzO3Rug/sendMessage',
    ['method'] = 'POST',
    ['headers'] = {
        ['Content-Type'] = ('%s; boundary=%s'):format('multipart/form-data', boundary),
        ['Content-Length'] = #source[index]
    },
    ['source'] = ltn12.source.string(source[index]),
    ['sink'] = ltn12.sink.table(response)
}
print(table.concat(response))

Did you fixed that recently ? Or have I something wrong in my code?

EDIT: I did a test compiling from source and i got the same

daurnimator commented 5 years ago

Ah ha, yes that segfaults for me too! I think it's a bug in luaossl. Simply

local openssl_ctx = require "openssl.ssl.context"
local openssl_pkey = require "openssl.pkey"

local ctx = openssl_ctx.new("TLS", false)
ctx:setEphemeralKey(openssl_pkey.new{ type = "EC", curve = "prime256v1" })

Is enough to segfault.

daurnimator commented 5 years ago

Tracked in https://github.com/wahern/luaossl/issues/172

otgo commented 5 years ago

I tried your code, really this is enough:

local openssl_ctx = require "openssl.ssl.context"

Maybe in lua_open_xx ?

daurnimator commented 5 years ago

I tried your code, really this is enough:

local openssl_ctx = require "openssl.ssl.context"

Maybe in lua_open_xx ?

In that case you might be having a different issue. Could you share how you install lua-http's dependencies? And your OS/distro?

daurnimator commented 4 years ago

@otgo ?

otgo commented 4 years ago

I tried again and it works XD i dont know why... sorry, i was busy at work these months