Comments (14)
Can you try using the asyncfile
instead? readFile
will read the whole file at once.
from jester.
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.
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.
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.
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.
Alright changes are made :)
Gist
Still crashes...
from jester.
Works for me but it's incredibly slow.
from jester.
But that's because you're sending one byte at a time lol
from jester.
It seems that your url decoding doesn't work though. Maybe this is a Linux-only bug? Are you on Linux?
from jester.
Your project is impressive already by the way, and it uses so few lines of code too!
from jester.
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.
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.
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.
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)
- Relative staticDir behavior changed
- Double log output on http requests HOT 1
- Jester crashes when async sleeping HOT 5
- development on windows (httpbeast) HOT 1
- finally block getting called twice? HOT 3
- Support binding on IPv6 address
- `std/cgi` will be moved to nimble packages
- Runtime SIGBUS: HOT 1
- how to use the setCookie fuction? HOT 1
- Docs: adding sections
- formData identical keys
- Bump tag to give new version through nimble HOT 2
- Revert PR #171 which auto decodes URL parameters HOT 6
- The jester.nimble file needs to be updated for v0.6.0 HOT 2
- Cannot serve dynamic web pages HOT 1
- Jester is not working with nim version 2.0.0 HOT 4
- static folder not working HOT 3
- Fix for SIGSEGV: Illegal storage access from lib/system/orc.nim nimIncRefCyclic HOT 1
- [security issue] http 1.1 request smuggling HOT 1
- Error: type mismatch Expression: httpbeast.initSettings HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jester.