mirror of
https://github.com/Open-Argon/http.ar.git
synced 2026-10-28 09:43:55 +00:00
modify to support files formdata.ar
This commit is contained in:
1 parent
d026e46a34
commit
4ec96d68f0
7 files changed
+58
-77
No files matched your search
@@ -46,7 +46,7 @@ t.start()
|
||||
|
||||
let err404path = path.join([parent,"static", "404.html"])
|
||||
|
||||
app.use(http.static(path.join([parent,"static"])))
|
||||
app.use(http.static("/",path.join([parent,"static"])))
|
||||
|
||||
let getCanvas(req, res) = do
|
||||
res.json(canvas)
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
import "../../src" as http
|
||||
import "../../src/bleach" as bleach
|
||||
|
||||
let app = http.server()
|
||||
|
||||
app.use(http.logger)
|
||||
|
||||
app.use(http.formdata)
|
||||
|
||||
let err404path = path.join([path.parent(program.file.path),"static", "404.html"])
|
||||
|
||||
app.use(http.static(path.join([path.parent(program.file.path),"static"])))
|
||||
app.use(http.static("/",path.join([path.parent(program.file.path),"static"])))
|
||||
|
||||
let form(req, res) = do
|
||||
term.log(req, res)
|
||||
res.send("<pre>"+bleach.clean(req.buffer.header.to('string'))+bleach.clean(req.buffer.body.to('string'))+"</pre><form method='POST'><input type='text' name='test'><input type='submit'></form>")
|
||||
app.route("/form", ['POST'], form)
|
||||
if (req.method == "POST") do
|
||||
term.log(req.formdata)
|
||||
term.log(req.files)
|
||||
res.send("POST")
|
||||
return
|
||||
|
||||
res.send("<form method='POST' enctype='multipart/form-data'><input type='text' name='text'><input type='file' name='file'><input type='submit'></form>")
|
||||
app.route("/form", ['GET','POST'], form)
|
||||
|
||||
let err404(req, res) = do
|
||||
res.code = 404
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import "../../src" as http
|
||||
import "../../src/url" as url
|
||||
|
||||
let app = http.server()
|
||||
|
||||
let files = {'http.ar': path.join([path.parent(program.file.path), "modules", "http.tar.gz"])}
|
||||
|
||||
app.use(http.logger)
|
||||
|
||||
|
||||
let download(req, res) = do
|
||||
|
||||
let zipfile = files[req.query.name]
|
||||
res.sendFile(zipfile)
|
||||
app.route("/isotope-download", null, download)
|
||||
|
||||
let search(req, res) = do
|
||||
res.json({Name: "http.ar", Version: "1.0.0", URL: "http://localhost:3000/isotope-download?"+url.queryStr({name: "http.ar", version: "1.0.0"})})
|
||||
app.route("/isotope-search", null, search)
|
||||
|
||||
app.run()
|
||||
Binary file not shown.
+40
-49
@@ -1,54 +1,45 @@
|
||||
let formdata(req, res) = do
|
||||
let boundary = null
|
||||
if ("Content-Type" in req.headers) do
|
||||
let contentType = req.headers["Content-Type"]
|
||||
if (contentType.startsWith("multipart/form-data")) do
|
||||
let parts = contentType.split("; ")
|
||||
for (i from 0 to parts.length) do
|
||||
let part = parts[i]
|
||||
if (part.startsWith("boundary=")) do
|
||||
boundary = part.split("=")[1]
|
||||
break
|
||||
if (boundary == null) do
|
||||
res.next()
|
||||
return
|
||||
let body = req.body
|
||||
let parts = body.split("--"+boundary)
|
||||
let data = {}
|
||||
let __multipart(req, res) = do
|
||||
let boundary = buffer().from(req.headers["Content-Type"].split("boundary=")[1])
|
||||
let newLineSplit = buffer().from("\r\n\r\n")
|
||||
let parts = req.buffer.body.split(boundary)
|
||||
for (i from 0 to parts.length) do
|
||||
let part = parts[i]
|
||||
if (part == "" || part == "--") do
|
||||
continue
|
||||
let lines = part.split("\r\n")
|
||||
let str = (parts[i].to("string"))
|
||||
if (str == "" || str=="--" || str=="--\r\n") continue
|
||||
str = null
|
||||
let headers = {}
|
||||
let body = ""
|
||||
for (j from 0 to lines.length) do
|
||||
let line = lines[j]
|
||||
if (line == "") do
|
||||
body = lines.slice(j+1).join("\r\n")
|
||||
break
|
||||
let header = line.split(": ")
|
||||
let body = buffer()
|
||||
let lines = parts[i].splitN(newLineSplit, 2)
|
||||
let headerLines = lines[0].to("string").split("\r\n")
|
||||
for (j from 0 to headerLines.length) do
|
||||
let header = headerLines[j].splitN(": ", 2)
|
||||
if (header.length != 2) continue
|
||||
headers[header[0]] = header[1]
|
||||
if (lines.length != 2) continue
|
||||
body.append(lines[1].slice(0,lines[1].length-4))
|
||||
if ("Content-Disposition" in headers) do
|
||||
let disposition = headers["Content-Disposition"]
|
||||
let name = null
|
||||
let filename = null
|
||||
let dispositionParts = disposition.split("; ")
|
||||
for (j from 0 to dispositionParts.length) do
|
||||
let part = dispositionParts[j]
|
||||
if (part.startsWith("name=")) do
|
||||
name = part.split("=")[1]
|
||||
else if (part.startsWith("filename=")) do
|
||||
filename = part.split("=")[1]
|
||||
if (name == null) do
|
||||
continue
|
||||
if (filename == null) do
|
||||
data[name] = body
|
||||
else do
|
||||
data[name] = {
|
||||
"filename": filename,
|
||||
"contentType": headers["Content-Type"],
|
||||
"data": body
|
||||
}
|
||||
req.formdata = data
|
||||
let disposition = headers["Content-Disposition"].split("; ")
|
||||
if (disposition[0] == "form-data") do
|
||||
let name = json.parse(disposition[1].splitN("=", 2)[1])
|
||||
if (disposition.length >= 3) do
|
||||
req.files[name] = {}
|
||||
req.files[name].name = json.parse(disposition[2].splitN("=", 2)[1])
|
||||
req.files[name].type = headers["Content-Type"]
|
||||
req.files[name].data = body
|
||||
let save(path) = do
|
||||
let file = file.write(path)
|
||||
file.write(body)
|
||||
file.close()
|
||||
req.files[name].save = save
|
||||
else do
|
||||
req.formdata[name] = body.to("string")
|
||||
res.next()
|
||||
|
||||
|
||||
let formdata(req, res) = do
|
||||
req.formdata = {}
|
||||
req.files = {}
|
||||
|
||||
if (req.method != "POST") return res.next()
|
||||
if ("Content-Type" in req.headers && req.headers["Content-Type"].startswith("multipart/form-data")) do
|
||||
return __multipart(req, res)
|
||||
res.next()
|
||||
@@ -2,6 +2,7 @@ import "listener" as listener
|
||||
import "logger" as *
|
||||
import "static" as *
|
||||
import "cookie" as *
|
||||
import "formdata" as *
|
||||
|
||||
let server() = do
|
||||
let class = {port: 3000, numThreads: 1, callback: ()=term.log("Server running on port "+class.port)}
|
||||
|
||||
+5
-2
@@ -1,6 +1,9 @@
|
||||
let static(path) = do
|
||||
let static(root, path) = do
|
||||
let middleware(req, res) = do
|
||||
let pathsToTry = [path+req.path, path+req.path+"/index.html"]
|
||||
if (req.method != "GET") return res.next()
|
||||
if (not req.path.startswith(root)) return res.next()
|
||||
let reqPath = req.path[root.length:]
|
||||
let pathsToTry = [path+reqPath, path+reqPath+"/index.html"]
|
||||
for (i from 0 to pathsToTry.length) do
|
||||
try do
|
||||
res.sendFile(pathsToTry[i])
|
||||
|
||||
Reference in new issue
Block a user