Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "net/http/cgi"
- "net/http/httputil"
- "net/url"
- "os"
- "path/filepath"
- "strings"
- )
- func main() {
- f, err := os.OpenFile(LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
- if err != nil {
- fmt.Println(err)
- }
- defer f.Close()
- log.SetOutput(f)
- if err := cgi.Serve(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- // ヘッダをセット
- header := w.Header()
- header.Set("Content-Type", "text/html; charset=utf-8")
- // パスを取得
- proxyPath := r.FormValue("proxyPath")
- if !filepath.IsAbs(proxyPath) {
- fmt.Fprintf(w, "Invalid request")
- return
- }
- formValue := r.Form
- // パスをQueryStringから消去
- formValue.Del("proxyPath")
- address, err := loadAddress(r.URL.Path)
- if err != nil {
- log.Fatalln(err)
- }
- target, err := url.Parse(address)
- if err != nil {
- log.Fatalln(err)
- }
- r.URL, err = url.Parse(address + proxyPath + "?" + formValue.Encode())
- if err != nil {
- log.Fatalln(err)
- }
- proxy := httputil.NewSingleHostReverseProxy(target)
- proxy.ServeHTTP(w, r)
- })); err != nil {
- log.Fatalln(err)
- }
- }
- <!DOCTYPE html>
- <html>
- <head>
- <title>画像アップロードテスト</title>
- <meta charset="utf-8">
- </head>
- <body>
- <form method="post" action="/rev-proxy/?proxyPath=/upload" enctype="multipart/form-data">
- <input type="file" name="file">
- <input type="submit" value="送信">
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment