- Timestamp:
- Oct 15, 2019, 7:56:30 AM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Dockerfile
r93 r97 26 26 USER morty 27 27 28 ENV DEBUG=true 29 28 30 ENTRYPOINT ["/usr/local/morty/morty"] -
trunk/morty.go
r96 r97 37 37 const VERSION = "v0.2.0" 38 38 39 const MAX_REDIRECT_COUNT = 5 39 var DEBUG = os.Getenv("DEBUG") 40 if DEBUG == "true" { 41 DEBUG = true 42 } else { 43 DEBUG = false 44 } 40 45 41 46 var CLIENT *fasthttp.Client = &fasthttp.Client{ … … 290 295 } 291 296 292 p.ProcessUri(ctx, string(requestURI), 0) 293 } 294 295 func (p *Proxy) ProcessUri(ctx *fasthttp.RequestCtx, requestURI string, redirectCount int) { 296 parsedURI, err := url.Parse(requestURI) 297 parsedURI, err := url.Parse(string(requestURI)) 297 298 298 299 if err != nil { … … 314 315 requestURIStr := string(requestURI) 315 316 316 log.Println("getting", requestURIStr) 317 if DEBUG { 318 log.Println("getting", requestURIStr) 319 } 317 320 318 321 req.SetRequestURI(requestURIStr) … … 345 348 loc := resp.Header.Peek("Location") 346 349 if loc != nil { 347 log.Println("redirect to", string(loc))348 if ctx.IsGet() {349 // GET method: Morty follows the redirect350 if redirectCount < MAX_REDIRECT_COUNT {351 p.ProcessUri(ctx, string(loc), redirectCount+1)352 } else{353 p.serveMainPage(ctx, 310, errors.New("Too many redirects"))350 rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI} 351 url, err := rc.ProxifyURI(loc) 352 if err == nil { 353 ctx.SetStatusCode(resp.StatusCode()) 354 ctx.Response.Header.Add("Location", url) 355 if DEBUG { 356 log.Println("redirect to", string(loc)) 354 357 } 355 358 return 356 } else {357 // Other HTTP methods: Morty does NOT follow the redirect358 rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI}359 url, err := rc.ProxifyURI(loc)360 if err == nil {361 ctx.SetStatusCode(resp.StatusCode())362 ctx.Response.Header.Add("Location", url)363 return364 }365 359 } 366 360 } … … 530 524 out.Write([]byte(uri)) 531 525 startIndex = urlEnd 532 } else {526 } else if DEBUG { 533 527 log.Println("cannot proxify css uri:", string(css[urlStart:urlEnd])) 534 528 } … … 551 545 err := decoder.Err() 552 546 if err != io.EOF { 553 log.Println("failed to parse HTML :")547 log.Println("failed to parse HTML") 554 548 } 555 549 break … … 793 787 if uri, err := rc.ProxifyURI(attrValue); err == nil { 794 788 fmt.Fprintf(out, " %s=\"%s\"", attrName, uri) 795 } else {789 } else if DEBUG { 796 790 log.Println("cannot proxify uri:", string(attrValue)) 797 791 } … … 943 937 _, err := hex.Decode(h, hashMsg) 944 938 if err != nil { 945 log.Println("hmac error:", err) 939 if DEBUG { 940 log.Println("hmac error:", err) 941 } 946 942 return false 947 943 } … … 969 965 ctx.Write([]byte(MORTY_HTML_PAGE_START)) 970 966 if err != nil { 971 log.Println("error:", err) 967 if DEBUG { 968 log.Println("error:", err) 969 } 972 970 ctx.Write([]byte("<h2>Error: ")) 973 971 ctx.Write([]byte(html.EscapeString(err.Error())))
Note:
See TracChangeset
for help on using the changeset viewer.