Giter VIP home page Giter VIP logo

Comments (14)

dom96 avatar dom96 commented on July 26, 2024

Can you try using the asyncfile instead? readFile will read the whole file at once.

from jester.

TheAnonymous avatar TheAnonymous commented on July 26, 2024

Ok so this is my code

    get "/@filename":
      var filename = @"filename"
      var file = openAsync(filename, fmRead)
      file.setFilePos(0)
      let data = await file.readAll()
      file.close()
      resp(data, "application")

Interestingly enough this works for small files (like 10 lines of text or something) but when I try to send big files I get this:

Jester is making jokes at http://localhost:8080
get /
  200 OK {Content-Length: 1680, Content-Type: text/html}
get /windows10.iso
Traceback (most recent call last)
uploader.nim(118)        uploader
uploader.nim(80)         default
asyncdispatch.nim(1540)  runForever
asyncdispatch.nim(983)   poll
asyncdispatch.nim(1091)  cb
asyncdispatch.nim(186)   complete
asyncdispatch.nim(1199)  cb
os.nim(1596)             recvLineIntoIter
asyncdispatch.nim(195)   complete
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1199)  cb
asynchttpserver.nim(222) processClientIter
jester.nim(308)          :anonymous
asyncdispatch.nim(1212)  handleHTTPRequest
(1874 calls omitted) ...
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1204)  cb
asyncdispatch.nim(224)   callback=
asyncdispatch.nim(1199)  cb
asyncfile.nim(236)       readAllIter
asyncdispatch.nim(253)   read
gc.nim(465)              newObjNoInit
gc.nim(440)              rawNewObj
alloc.nim(599)           rawAlloc
avltree.nim(61)          add
avltree.nim(61)          add
avltree.nim(61)          add
avltree.nim(59)          add
avltree.nim(61)          add
avltree.nim(61)          add
avltree.nim(59)          add
avltree.nim(61)          add
avltree.nim(59)          add
avltree.nim(61)          add
avltree.nim(59)          add
avltree.nim(61)          add
avltree.nim(61)          add
avltree.nim(56)          add
alloc.nim                allocAvlNode
Stack overflow

from jester.

dom96 avatar dom96 commented on July 26, 2024

You haven't changed a thing. You're still reading the whole file into memory at once. Use await file.read(8000), send the data (via response.client.send()), then loop and do it again. You have to use Jester's sendHeaders for this.

from jester.

TheAnonymous avatar TheAnonymous commented on July 26, 2024

Ok so I tried this

get "/@filename":
      await response.sendHeaders()
      var filename = @"filename"
      filename = uriToString(filename)
      var file = openAsync(filename, fmRead)
      file.setFilePos(0)
      var data = await file.read(8000)
      var pos: int64 = 0
      var pos_tmp: int64 = 1
      while pos < pos_tmp:
        echo pos
        pos = pos_tmp
        await response.client.send(data)
        data = await file.read(8000)
        pos_tmp = file.getFilePos()
      file.close()
      response.client.close()

And at the first request it works I get the file in the browser but the server prints out

  TCActionRaw

if I reload the page this happens


get /HNI%200001_MPO.JPG
Traceback (most recent call last)
uploader.nim(123)        uploader
uploader.nim(85)         default
asyncdispatch.nim(1540)  runForever
asyncdispatch.nim(991)   poll
asyncdispatch.nim(1123)  cb
asyncdispatch.nim(195)   complete
asyncdispatch.nim(1199)  cb
asyncnet.nim(274)        sendIter
asyncdispatch.nim(195)   complete
asyncdispatch.nim(1199)  cb
jester.nim(92)           sendHeadersIter
asyncdispatch.nim(186)   complete
asyncdispatch.nim(1199)  cb
jester.nim(119)          sendHeadersIter
asyncdispatch.nim(195)   complete
asyncdispatch.nim(1211)  cb
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

and of course the server exits. Am I doing something obvious wrong? Because I'm a nim noob and there are not that much examples for asyncfile...
Also I have two questions and I don't know if this is the right place to ask so redirect me if not.

  • Is there a more elegant version of asking if end of file is reached than pos and pos_tmp ?
  • Before I tried the async stuff I sent the response with resp(file, "application") but since I use await response.client.send(data) it seems the browser tries to open the file instead of downloading it. Is there a way to tell the browser to download it instead of opening?

Cheers and thanks for your help.

from jester.

dom96 avatar dom96 commented on July 26, 2024

Take a look at the source code of the asyncfile module for clues, for example here is how readAll is implemented: https://github.com/nim-lang/Nim/blob/master/lib/pure/asyncfile.nim#L232. You just need to check whether data is "".

As for your second question, you just need to specify a specific content-type in your call to sendHeaders. I'm assuming "application" will work?

I'm not sure what the cause of the crash is, but please try making those changes. If it still crashes gist all your code and i'll take a look.

from jester.

TheAnonymous avatar TheAnonymous commented on July 26, 2024

Alright changes are made :)
Gist
Still crashes...

from jester.

dom96 avatar dom96 commented on July 26, 2024

Works for me but it's incredibly slow.

from jester.

dom96 avatar dom96 commented on July 26, 2024

But that's because you're sending one byte at a time lol

from jester.

dom96 avatar dom96 commented on July 26, 2024

It seems that your url decoding doesn't work though. Maybe this is a Linux-only bug? Are you on Linux?

from jester.

dom96 avatar dom96 commented on July 26, 2024

Your project is impressive already by the way, and it uses so few lines of code too!

from jester.

TheAnonymous avatar TheAnonymous commented on July 26, 2024

Well I read 1 byte a time because it tried to debug, normally I would choose 4000 or so...
No I'm working on OSX 10.10.3 .
Url decoding will be changed to "cgi", decodeUrl(). I didn't find the method at the time I wrote this functions...
I made a video of the bug so you may understand my problem better.
OK so I did some test with Fedora 22 in a virtual machine and I don't know why but it works like charm :/
I used the same nim and jester versions for OSX and Fedora.
On which OS did you tested it?
And thanks again for your help!
Greetings Jakob

from jester.

dom96 avatar dom96 commented on July 26, 2024

I can reproduce it on OSX. Looks like yet another issue with OSX, I would guess it's an issue with the compiler though not Jester.

from jester.

TheAnonymous avatar TheAnonymous commented on July 26, 2024

So I played a bit and tried:

  • Compiled it on OSX with GCC-5 and error stays
  • Tried all different GC options and error stays
  • Tried to compile it with cpp comand and get
    SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Do you have any other idea what I can try?
Any other thing I can do to help?

from jester.

dom96 avatar dom96 commented on July 26, 2024

Use GDB and see where it crashes. I tried, but it turns out that Mac OS X makes things more difficult than they should be.

from jester.

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.