-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Adds CORS Support #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -457,6 +457,22 @@ the response was sent. This would then result in s.httpTransport.RoundTrip(clean | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| an EOF error when it tried to re-use that TCP connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (s *Server) forwardHttp(w http.ResponseWriter, req *http.Request, newurl string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Set the Access-Control-Allow-Origin header, based on allow-list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.errorLog.Infof("Forwarding from \"%v\"", req.Header.Get("Origin")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _, ok := s.configHttp.Origins[req.Header.Get("Origin")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Origin", req.Header.Get("Origin")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Credentials", "true") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle preflight OPTIONS requests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if req.Method == "OPTIONS" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.WriteHeader(http.StatusOK) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+460
to
+474
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to minimize the impact on our standard config.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleaned, err := http.NewRequest(req.Method, newurl, req.Body) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Test Folder | ||
|
|
||
| This folder contains utilities for testing CORS (Cross-Origin Resource Sharing) settings on a remote server. | ||
|
|
||
| ## Files | ||
|
|
||
| ### servefile.go | ||
|
|
||
| A minimal Go HTTP server that serves the `test.html` file on | ||
| `http://localhost:8080`. This allows you to easily load the test page in your | ||
| browser. | ||
|
|
||
| ### test.html | ||
|
|
||
| A simple HTML page with JavaScript that lets you specify a remote server URL and | ||
| make a cross-origin request to it. This helps you verify if your target server's | ||
| CORS settings are correct by observing the response and any errors. | ||
| Make sure your of your working directory to avoid path errors. | ||
|
|
||
| ## Usage | ||
| 1. Run `servefile.go` with `go run servefile.go`. | ||
| 2. Open `http://localhost:8080` in your browser. | ||
| 3. Enter the remote server URL you want to test and click "Test CORS". | ||
| 4. Observe the results and adjust your target server's CORS configuration as needed. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| ) | ||
|
|
||
| type TS struct { | ||
| } | ||
|
|
||
| func (TS) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| http.ServeFile(w, r, "test.html") | ||
| } | ||
|
|
||
| func main() { | ||
| fmt.Println("Starting static file server...") | ||
| testServe := TS{} | ||
| fmt.Println("CORS test web page running on http://localhost:8080") | ||
| http.ListenAndServe(":8080", testServe) | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like fluff, but please move this file one level deeper in case we need to use this folder for other more involved testing setups New Dir: But then we can probably rename the file to |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>CORS Test Utility</title> | ||
| <style> | ||
| body { font-family: Arial, sans-serif; margin: 2em; } | ||
| input, button { font-size: 1em; margin: 0.5em 0; } | ||
| #result { margin-top: 1em; white-space: pre-wrap; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h2>CORS Test Utility</h2> | ||
| <label for="url">Remote Server URL:</label> | ||
| <input type="text" id="url" size="50" placeholder="https://your-server.com/api/test"> | ||
| <button onclick="testCORS()">Test CORS</button> | ||
| <div id="result"></div> | ||
| <script> | ||
| function testCORS() { | ||
| const url = document.getElementById('url').value; | ||
| const resultDiv = document.getElementById('result'); | ||
| resultDiv.textContent = 'Testing...'; | ||
| fetch(url, { method: 'GET', mode: 'cors' }) | ||
| .then(response => { | ||
| resultDiv.textContent = `Status: ${response.status}\nCORS appears to be working.`; | ||
| return response.text(); | ||
| }) | ||
| .then(text => { | ||
| resultDiv.textContent += `\nResponse Body:\n${text}`; | ||
| }) | ||
| .catch(error => { | ||
| resultDiv.textContent = `Error: ${error}\nLikely a CORS issue or network error.`; | ||
| }); | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you move the test files one level deeper then the exe that gets created on linux will be
cors. So please add this to help people on linux that accidentally rango buildin the folder