Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x.vweb: add cors middleware (#20713)
- Loading branch information
Showing
3 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import time | ||
import x.vweb | ||
|
||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS | ||
// and https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests | ||
// > Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows | ||
// > a server to indicate any origins (domain, scheme, or port) other than its own from | ||
// > which a browser should permit loading resources... | ||
|
||
// Usage: do `./v run examples/xvweb/cors/` to start the app, | ||
// then check the headers in another shell: | ||
// | ||
// 1) `curl -vvv -X OPTIONS http://localhost:45678/time` | ||
// 2) `curl -vvv -X POST http://localhost:45678/time` | ||
|
||
pub struct Context { | ||
vweb.Context | ||
} | ||
|
||
pub struct App { | ||
vweb.Middleware[Context] | ||
} | ||
|
||
// time is a simple POST request handler, that returns the current time. It should be available | ||
// to JS scripts, running on arbitrary other origins/domains. | ||
@[post] | ||
pub fn (app &App) time(mut ctx Context) vweb.Result { | ||
return ctx.json({ | ||
'time': time.now().format_ss_milli() | ||
}) | ||
} | ||
|
||
fn main() { | ||
println(" | ||
To test, if CORS works, copy this JS snippet, then go to for example https://stackoverflow.com/ , | ||
press F12, then paste the snippet in the opened JS console. You should see the vweb server's time: | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onload = function(data) { | ||
console.log('xhr loaded'); | ||
console.log(xhr.response); | ||
}; | ||
xhr.open('POST', 'http://localhost:45678/time'); | ||
xhr.send(); | ||
") | ||
|
||
mut app := &App{} | ||
|
||
// use vweb's cors middleware to handle CORS requests | ||
app.use(vweb.cors[Context](vweb.CorsOptions{ | ||
// allow CORS requests from every domain | ||
origins: ['*'] | ||
// allow CORS requests with the following request methods: | ||
allowed_methods: [.get, .head, .patch, .put, .post, .delete] | ||
})) | ||
|
||
vweb.run[App, Context](mut app, 45678) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import x.vweb | ||
import net.http | ||
import os | ||
import time | ||
|
||
const port = 13012 | ||
const localserver = 'http://localhost:${port}' | ||
const exit_after = time.second * 10 | ||
const allowed_origin = 'https://vlang.io' | ||
const cors_options = vweb.CorsOptions{ | ||
origins: [allowed_origin] | ||
allowed_methods: [.get, .head] | ||
} | ||
|
||
pub struct Context { | ||
vweb.Context | ||
} | ||
|
||
pub struct App { | ||
vweb.Middleware[Context] | ||
mut: | ||
started chan bool | ||
} | ||
|
||
pub fn (mut app App) before_accept_loop() { | ||
app.started <- true | ||
} | ||
|
||
pub fn (app &App) index(mut ctx Context) vweb.Result { | ||
return ctx.text('index') | ||
} | ||
|
||
@[post] | ||
pub fn (app &App) post(mut ctx Context) vweb.Result { | ||
return ctx.text('post') | ||
} | ||
|
||
fn testsuite_begin() { | ||
os.chdir(os.dir(@FILE))! | ||
spawn fn () { | ||
time.sleep(exit_after) | ||
assert true == false, 'timeout reached!' | ||
exit(1) | ||
}() | ||
|
||
mut app := &App{} | ||
app.use(vweb.cors[Context](cors_options)) | ||
|
||
spawn vweb.run_at[App, Context](mut app, port: port, timeout_in_seconds: 2) | ||
// app startup time | ||
_ := <-app.started | ||
} | ||
|
||
fn test_valid_cors() { | ||
x := http.fetch(http.FetchConfig{ | ||
url: localserver | ||
method: .get | ||
header: http.new_header_from_map({ | ||
.origin: allowed_origin | ||
}) | ||
})! | ||
|
||
assert x.status() == .ok | ||
assert x.body == 'index' | ||
} | ||
|
||
fn test_preflight() { | ||
x := http.fetch(http.FetchConfig{ | ||
url: localserver | ||
method: .options | ||
header: http.new_header_from_map({ | ||
.origin: allowed_origin | ||
}) | ||
})! | ||
assert x.status() == .ok | ||
assert x.body == 'ok' | ||
|
||
assert x.header.get(.access_control_allow_origin)! == allowed_origin | ||
if _ := x.header.get(.access_control_allow_credentials) { | ||
assert false, 'Access-Control-Allow-Credentials should not be present the value is `false`' | ||
} | ||
assert x.header.get(.access_control_allow_methods)! == 'GET, HEAD' | ||
} | ||
|
||
fn test_invalid_origin() { | ||
x := http.fetch(http.FetchConfig{ | ||
url: localserver | ||
method: .get | ||
header: http.new_header_from_map({ | ||
.origin: 'https://google.com' | ||
}) | ||
})! | ||
|
||
assert x.status() == .forbidden | ||
} | ||
|
||
fn test_invalid_method() { | ||
x := http.fetch(http.FetchConfig{ | ||
url: '${localserver}/post' | ||
method: .post | ||
header: http.new_header_from_map({ | ||
.origin: allowed_origin | ||
}) | ||
})! | ||
|
||
assert x.status() == .method_not_allowed | ||
} |