Discussion:
Help can't work out what is happening
somealiassoitwillstopaskingqs
2013-03-29 11:48:34 UTC
Permalink
Hi all,

I have been playing around with io for a little while now. I have been trying to understand it better by expanding the example given for a basic webserver

For some reason the if statement testing for the request path ending in a / seems to completely stop it working. i can't work out what i am doing wrong.

port := 8000

WebRequest := Object clone do(
handleSocket := method(aSocket,
"[server] handling request\n" print
aSocket streamReadNextChunk
request := aSocket readBuffer
request print
requestPath := request betweenSeq("GET "," HTTP")
"[server] #{requestPath}\n" interpolate print
lastChar := requestPath exSlice(-1)
lastChar print
if(lastChar == "/", requestPath := "/index.html")
requestPath print
f := File with(requestPath)
if(f exists,
f streamTo(aSocket)
,
aSocket streamWrite("not found")
)
aSocket close
)
)

WebServer := Server clone do(
setPort(8000)
handleSocket := method(aSocket,
WebRequest clone asyncSend(handleSocket(aSocket))
)
)
"[server] starting on port: #{port}\n" interpolate print

WebServer start



Also the prints around the if don't seem to do anything.
Wesley Teal
2013-03-30 18:33:03 UTC
Permalink
Post by somealiassoitwillstopaskingqs
For some reason the if statement testing for the request path ending in a / seems to completely stop it working. i can't work out what i am doing wrong.
Hi some,
I'm really not sure what's wrong with your code, except that for some reason the slash in
requestPath := "/index.html" seems to be really confusing the Io vm. If you remove it, your prints should work and requestPath should be reassigned as you wish. Io's web handling appears to automatically prepend the root directory to the beginnings of requests.
Post by somealiassoitwillstopaskingqs
port := 8000
WebRequest := Object clone do(
handleSocket := method(aSocket,
"[server] handling request\n" print
aSocket streamReadNextChunk
request := aSocket readBuffer
request print
requestPath := request betweenSeq("GET "," HTTP")
"[server] #{requestPath}\n" interpolate print
lastChar := requestPath exSlice(-1)
lastChar print
if(lastChar == "/", requestPath := "/index.html")
^-- here's your problem, delete that slash and all should be well.

As an aside, since requestPath has been instantiated already you could write
requestPath = "index.html" instead of requestPath := "index.html". It shouldn't make any difference, but the := is only required when you create an object, but isn't necessary for assigning a new value to it.
Post by somealiassoitwillstopaskingqs
requestPath print
^-- for whatever reason, this line doesn't seem to print until another request is made, so it comes out before all the header info of the next request rather than after the header info of the request it's referring to.
Post by somealiassoitwillstopaskingqs
f := File with(requestPath)
if(f exists,
f streamTo(aSocket)
,
aSocket streamWrite("not found")
)
aSocket close
)
)
WebServer := Server clone do(
setPort(8000)
handleSocket := method(aSocket,
WebRequest clone asyncSend(handleSocket(aSocket))
)
)
"[server] starting on port: #{port}\n" interpolate print
WebServer start
Also the prints around the if don't seem to do anything.
Should work now. I hope this helps.

Best,
Wes
somealiassoitwillstopaskingqs
2013-03-31 19:33:34 UTC
Permalink
Thanks a lot. Definitely a bit of weird behaviour.
Post by Wesley Teal
Post by somealiassoitwillstopaskingqs
For some reason the if statement testing for the request path ending in a / seems to completely stop it working. i can't work out what i am doing wrong.
Hi some,
I'm really not sure what's wrong with your code, except that for some reason the slash in
requestPath := "/index.html" seems to be really confusing the Io vm. If you remove it, your prints should work and requestPath should be reassigned as you wish. Io's web handling appears to automatically prepend the root directory to the beginnings of requests.
Post by somealiassoitwillstopaskingqs
port := 8000
WebRequest := Object clone do(
handleSocket := method(aSocket,
"[server] handling request\n" print
aSocket streamReadNextChunk
request := aSocket readBuffer
request print
requestPath := request betweenSeq("GET "," HTTP")
"[server] #{requestPath}\n" interpolate print
lastChar := requestPath exSlice(-1)
lastChar print
if(lastChar == "/", requestPath := "/index.html")
^-- here's your problem, delete that slash and all should be well.
As an aside, since requestPath has been instantiated already you could write
requestPath = "index.html" instead of requestPath := "index.html". It shouldn't make any difference, but the := is only required when you create an object, but isn't necessary for assigning a new value to it.
Post by somealiassoitwillstopaskingqs
requestPath print
^-- for whatever reason, this line doesn't seem to print until another request is made, so it comes out before all the header info of the next request rather than after the header info of the request it's referring to.
Post by somealiassoitwillstopaskingqs
f := File with(requestPath)
if(f exists,
f streamTo(aSocket)
,
aSocket streamWrite("not found")
)
aSocket close
)
)
WebServer := Server clone do(
setPort(8000)
handleSocket := method(aSocket,
WebRequest clone asyncSend(handleSocket(aSocket))
)
)
"[server] starting on port: #{port}\n" interpolate print
WebServer start
Also the prints around the if don't seem to do anything.
Should work now. I hope this helps.
Best,
Wes
Loading...