Skip to content

Commit 4a5a304

Browse files
committed
created myrequest
1 parent a0a6e24 commit 4a5a304

5 files changed

Lines changed: 78 additions & 46 deletions

File tree

README.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,25 @@ end
6969
end
7070

7171
@route POST|PUT|DELETE "/" begin
72-
println("params: ",q.params)
73-
println("query: ",q.query)
74-
println("body: ",q.body)
72+
println("params: ",req.params)
73+
println("query: ",req.query)
74+
println("body: ",req.body)
7575

7676
res.headers["Content-Type"]= "text/plain"
7777

7878
"I did something!"
7979
end
8080

81-
Get("/data", (q,req,res)->(begin
81+
Get("/data", (req,res)->(begin
8282
res.headers["Content-Type"]= "text/plain"
8383
u*"data"
8484
end))
8585

8686

87-
Post("/data", (q,req,res)->(begin
88-
println("params: ",q.params)
89-
println("query: ",q.query)
90-
println("body: ",q.body)
87+
Post("/data", (req,res)->(begin
88+
println("params: ",req.params)
89+
println("query: ",req.query)
90+
println("body: ",req.body)
9191
res.headers["Content-Type"]= "text/plain"
9292
global u="bye"
9393
"I did something!"
@@ -100,20 +100,35 @@ server.start(Dict("host" => "127.0.0.1","port" => 8000))
100100

101101
Features available in the current release
102102
------------------
103+
### Data stored on request (req)
104+
```
105+
query # data from the query url
106+
params # data from the regular expresion
107+
body # body of the request in dict or plane text
108+
version # the protocol version
109+
headers # the headers sent by the client
110+
```
111+
### Data stored on response (req)
112+
```
113+
status
114+
headers
115+
body
116+
```
117+
103118
### Parameters dictionary
104119
```julia
105-
# :data> does not accept special symbols (!,#,$,etc)
106120
@route GET "/get/:data>" begin
107121
# matches "GET /get/foo" and "GET /get/bar"
108-
# q.params["data"] is 'foo' or 'bar'
109-
"get this back: "*q.params["data"]
122+
# not accept special symbols (!,#,$,etc)
123+
# req.params["data"] is 'foo' or 'bar'
124+
"get this back: "*req.params["data"]
110125
end
111126

112127
# it is possible to use regular expressions, enclosing them always between '(' ')'
113128
@route GET "/regex/(\\w+\\d+)" begin
114129
# matches "GET /regex/test1" and "GET /regex/test125"
115-
# q.params[1] is 'test1' or 'test125'
116-
"datos $(q.params[1])"
130+
# req.params[1] is 'test1' or 'test125'
131+
"datos $(req.params[1])"
117132
end
118133
```
119134
### url query dictionary
@@ -122,8 +137,8 @@ end
122137
@route POST|PUT|DELETE "/" begin
123138
res.headers["Content-Type"]= "text/plain"
124139
# matches "POST /?title=foo&author=bar"
125-
title = q.query["title"]
126-
author = q.query["author"]
140+
title = req.query["title"]
141+
author = req.query["author"]
127142
"I did something!"
128143
end
129144
```
@@ -135,7 +150,7 @@ Payload
135150
```julia
136151
@route POST|PUT|DELETE "/" begin
137152
res.headers["Content-Type"]= "text/plain"
138-
res.body = "Payload data "*q.body["data1"]
153+
res.body = "Payload data "*req.body["data1"]
139154
end
140155
```
141156

@@ -148,7 +163,7 @@ Payload
148163
```julia
149164
@route POST|PUT|DELETE "/" begin
150165
res.headers["Content-Type"]= "text/plain"
151-
"Payload data "*q.body["Data"]["Data1"]
166+
"Payload data "*req.body["Data"]["Data1"]
152167
end
153168
```
154169

src/base.jl

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ mutable struct myresponse
22
status::Int
33
headers::Dict
44
body::String
5+
function myresponse(code)
6+
new(code,Dict(),"")
7+
end
58
end
69

7-
mutable struct Data
10+
mutable struct myrequest
811
query::Dict
9-
params::Any
12+
params::RegexMatch
1013
body::Any
14+
version::VersionNumber
15+
headers::Array
16+
function myrequest()
17+
new(Dict(),match(r"\s"," "),"",VersionNumber(1),[])
18+
end
1119
end
1220

13-
struct Fram
21+
struct App
1422
notfound::Function
1523
start::Function
1624
useCORS::Function
@@ -27,7 +35,7 @@ elseif Sys.iswindows() && root[end]=='\\'
2735
end
2836
exten="\"\""::AbstractString
2937
notfound_message = "NotFound"::String
30-
q=Data(Dict(),"","")
38+
3139

3240
function File(file::String)
3341
path = normpath(root, file)
@@ -47,32 +55,37 @@ function handler(request::HTTP.Messages.Request)
4755
data = split(request.target,"?")
4856
url=data[1]
4957
searchroute = request.method*url
50-
if (length(data)>1) q.query= HTTP.queryparams(data[2]) end
51-
response = myresponse(200,Dict(),"")
58+
59+
my_request=myrequest()
60+
my_request.version=request.version
61+
my_request.headers=request.headers
62+
if (length(data)>1) my_request.query= HTTP.queryparams(data[2]) end
63+
64+
response = myresponse(200)
5265

5366
if ((request.method=="POST" )||(request.method=="PUT" )||(request.method=="PATCH"))
5467
header_content_type = HTTP.header(request, "Content-Type")
5568
if(length(header_content_type)>0)
56-
q.body= getindex(formats, header_content_type)(String(request.body))
69+
my_request.body= getindex(formats, header_content_type)(String(request.body))
5770
else
58-
q.body = getindex(formats, "*/*")(String(request.body))
71+
my_request.body = getindex(formats, "*/*")(String(request.body))
5972
end
6073
end
6174

6275
if (searchroute in routes_array)
63-
response.body = getindex(routes, searchroute)(q,request,response)
76+
response.body = getindex(routes, searchroute)(my_request,response)
6477
else
6578
pattern = searchroute_regex(searchroute)
6679
if (typeof(pattern) <:Regex)
67-
q.params = match(pattern,searchroute)
80+
my_request.params = match(pattern,searchroute)
6881
_function = getindex(routes_patterns,pattern)
69-
response.body = _function(q,request,response)
82+
response.body = _function(my_request,response)
7083
sal = collect((m.match for m = eachmatch(Regex("{{([a-z])+}}"), response.body)))
7184
for i in sal
72-
response.body = replace(response.body,Regex(i) => q.params["$(i[3:end-2])"])
85+
response.body = replace(response.body,Regex(i) => my_request.params["$(i[3:end-2])"])
7386
end
7487
else
75-
response.body = getindex(routes, "notfound")(q,request,response)
88+
response.body = getindex(routes, "notfound")(my_request,response)
7689
end
7790
end
7891
responsefinal = HTTP.Response(response.status,my_headers, body=response.body)
@@ -126,7 +139,7 @@ function app()
126139
end
127140

128141
@info("App created")
129-
return Fram(notfound,start,useCORS,webserverfiles,webserverpath)
142+
return App(notfound,start,useCORS,webserverfiles,webserverpath)
130143
end
131144

132145
#HSTS HTTP.setheader(response,"Strict-Transport-Security" => "max-age=10886400; includeSubDomains; preload"

src/routes.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ HEAD = "HEAD"
99
OPTIONS = "OPTIONS"
1010
PATCH = "PATCH"
1111

12-
function NotFound(q,req,res)
12+
function NotFound(req,res)
1313
res.status = 404
1414
return notfound_message
1515
end
@@ -37,15 +37,15 @@ end
3737

3838
macro page(exp1,exp2)
3939
quote
40-
createurl("GET"*$exp1,(q,req,res)->$exp2)
40+
createurl("GET"*$exp1,(req,res)->$exp2)
4141
end
4242
end
4343

4444
macro route(exp1,exp2,exp3)
4545
quote
4646
verbs= split($exp1,"|")
4747
for i=verbs
48-
createurl(i*$exp2,(q,req,res)->$exp3)
48+
createurl(i*$exp2,(req,res)->$exp3)
4949
end
5050
end
5151
end

src/webserver.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function files(arch::Array{Any,1})
77
ext= split(roop,".")
88
if(length(ext)>1) extension=mimetypes[ext[2]] end
99
data = File(roop[2:end])
10-
createurl("GET"*roop,(q,req,res)->(begin
10+
createurl("GET"*roop,(req,res)->(begin
1111
res.headers["Content-Type"]= extension
1212
res.status = 200
1313
res.body= data

test/runtests.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,38 @@ u="hello"
3333
end
3434

3535
@route GET "/regex/(\\w+\\d+)" begin
36-
"datos $(q.params[1])"
36+
37+
println("req.version ",req.version)
38+
println("req.headers ",req.headers)
39+
40+
"datos $(req.params[1])"
3741
end
3842

3943
@route POST "/post" begin
4044
res.body = "I did something!"
4145
end
4246

4347
@route POST|PUT|DELETE "/" begin
44-
println("params: ",q.params)
45-
println("query: ",q.query)
46-
println("body: ",q.body)
48+
println("params: ",req.params)
49+
println("query: ",req.query)
50+
println("body: ",req.body)
4751

4852
res.headers["Content-Type"]= "text/plain"
4953

5054
"I did something!"
5155
end
5256

53-
Get("/data", (q,req,res)->(begin
57+
Get("/data", (req,res)->(begin
5458
res.headers["Content-Type"]= "text/plain"
55-
println("params: ",q.params)
56-
println("query: ",q.query)
59+
println("params: ",req.params)
60+
println("query: ",req.query)
5761
u*"data"
5862
end))
5963

60-
Post("/data", (q,req,res)->(begin
61-
println("params: ",q.params)
62-
println("query: ",q.query)
63-
println("body: ",q.body)
64+
Post("/data", (req,res)->(begin
65+
println("params: ",req.params)
66+
println("query: ",req.query)
67+
println("body: ",req.body)
6468
res.headers["Content-Type"]= "text/plain"
6569
global u="bye"
6670
"I did something!"

0 commit comments

Comments
 (0)