GithubHelp home page GithubHelp logo

Better error reporting. about lua-openssl HOT 9 CLOSED

zhaozg avatar zhaozg commented on July 17, 2024
Better error reporting.

from lua-openssl.

Comments (9)

zhaozg avatar zhaozg commented on July 17, 2024

please consider d774cab,

and you can try https://github.com/zhaozg/lua-openssl/blob/master/ldoc/openssl.lua#L28-L33 and d774cab#diff-1414f3bd5705103f433eca15f5ef60b7R315

from lua-openssl.

moteus avatar moteus commented on July 17, 2024

Thanks. It works.
Can you help me to understand right way to implement error handling
E.g. this is function to implement SSL decoding (bases on your luv ssl example)

-- self._out - BIO_mem
-- self._ssl - SSL context
-- self._obuffer - buffer that contain data that shuld be encoded

-- this function returns next chunk of encoded data
function SSLDecoder:output() 
  -- Here we should clear OpenSSL error queue?
  ssl.last_error(true)

  local chunk, err = self._out:read()
  if not chunk then
    -- shuld we check err or just call `last_error`
    local code, reason = ssl.last_error()
    if code then --[[handle error]]  end
  elseif  #chunk > 0 then return chunk end

  while not self._obuffer:empty() do
    -- next chunk do decode
    chunk = self._obuffer:read_some(OCHUNK_SIZE)

    local n, err = self._ssl:write(chunk)
    -- same as in case of `self._out:read()`?
   ...

You can check out my code here

from lua-openssl.

zhaozg avatar zhaozg commented on July 17, 2024

ok, my pleasure

from lua-openssl.

moteus avatar moteus commented on July 17, 2024

Questions in comments in code snippets.

from lua-openssl.

zhaozg avatar zhaozg commented on July 17, 2024

Sorry for can't give solution right now, but I can write some article to show how to handle it with lua-openssl in some days.

from lua-openssl.

moteus avatar moteus commented on July 17, 2024

Thanks. It would be halpful.

from lua-openssl.

zhaozg avatar zhaozg commented on July 17, 2024

https://github.com/zhaozg/lua-openssl#howto-5-handle-fail-or-error
https://github.com/zhaozg/lua-openssl/blob/master/ldoc/ssl.lua#L218

-- self._out - BIO_mem
-- self._ssl - SSL context
-- self._obuffer - buffer that contain data that shuld be encoded

-- this function returns next chunk of encoded data
function SSLDecoder:output() 
  ssl.error(true)   --better to clear error queue

  local chunk,  err = self._out:read()
  if chunk then return chunk  end --success

  if chunk==nil then -- ssl or syscall error,real error
       print('ERROR with:'..err)
  end
  assert(chunk==false)  --ssl IO need lua script to do something,
  if err==0 then
     print('SSL has been closed') --just connection really closed
     return
  end
  print('want to',err) --  want_read/write/x509_lookup/... SSL not fail, but want API do something

  local n, err = self._ssl:write(chunk)
  -- n is bytes count successfully write, for read that is input string,
  -- err same with read.

from lua-openssl.

moteus avatar moteus commented on July 17, 2024

I have some questions

self._out:read() is read from BIO_mem object so in local chunk, err = self._out:read()
chunk can be only nil or string but not false because it is not SSL function.

  local chunk,  err = self._out:read()

  if not chunk then -- Error and we get error code
       return nil, openssl.error(err) -- get error info
  end

  -- we can not get chunk == false because this is just BIO operation?
  assert(type(chunk) == 'string')

  -- success only if chunk not empty?
  -- I get chunk == '' while ssl:write returns false, 'want_read'
  if #chunk > 0 then return chunk  end

  while ... do
    -- here we try to encode next chunk from input stream and try read again
    chunk = .... next decoded chunk

    local n, err = self._ssl:write(chunk)

    if not n then
      if n == nil then -- ssl/syscall - check out last error
        return nil, openssl.error()
      end

      assert(n == false)

      if err==0 then
         print('SSL has been closed') --just connection really closed
         return nil, 'EOF'
      end

      -- is it possible get err == 'want_write' after write operation.
      -- if yes should I just increase chunk or write next chunk?

      return ''
    end

    -- repeat read from self._out:read()

from lua-openssl.

zhaozg avatar zhaozg commented on July 17, 2024

That is right.
self._out:read() is read from BIO_mem object so in local chunk, err = self._out:read()
chunk can be only nil or string but not false because it is not SSL function.

Your code is right too.
when bio:read() return string '', should continue to do something and retry read,

from lua-openssl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.