Changeset 97 in code for trunk


Ignore:
Timestamp:
Oct 15, 2019, 7:56:30 AM (6 years ago)
Author:
mathieu.brunot
Message:

:sparkles: Var to enable/disable debug logs

Signed-off-by: mathieu.brunot <mathieu.brunot@…>

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Dockerfile

    r93 r97  
    2626USER morty
    2727
     28ENV DEBUG=true
     29
    2830ENTRYPOINT ["/usr/local/morty/morty"]
  • trunk/morty.go

    r96 r97  
    3737const VERSION = "v0.2.0"
    3838
    39 const MAX_REDIRECT_COUNT = 5
     39var DEBUG = os.Getenv("DEBUG")
     40if DEBUG == "true" {
     41        DEBUG = true
     42} else {
     43        DEBUG = false
     44}
    4045
    4146var CLIENT *fasthttp.Client = &fasthttp.Client{
     
    290295        }
    291296
    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))
    297298
    298299        if err != nil {
     
    314315        requestURIStr := string(requestURI)
    315316
    316         log.Println("getting", requestURIStr)
     317        if DEBUG {
     318                log.Println("getting", requestURIStr)
     319        }
    317320
    318321        req.SetRequestURI(requestURIStr)
     
    345348                        loc := resp.Header.Peek("Location")
    346349                        if loc != nil {
    347                                 log.Println("redirect to", string(loc))
    348                                 if ctx.IsGet() {
    349                                         // GET method: Morty follows the redirect
    350                                         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))
    354357                                        }
    355358                                        return
    356                                 } else {
    357                                         // Other HTTP methods: Morty does NOT follow the redirect
    358                                         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                                                 return
    364                                         }
    365359                                }
    366360                        }
     
    530524                        out.Write([]byte(uri))
    531525                        startIndex = urlEnd
    532                 } else {
     526                } else if DEBUG {
    533527                        log.Println("cannot proxify css uri:", string(css[urlStart:urlEnd]))
    534528                }
     
    551545                        err := decoder.Err()
    552546                        if err != io.EOF {
    553                                 log.Println("failed to parse HTML:")
     547                                log.Println("failed to parse HTML")
    554548                        }
    555549                        break
     
    793787                if uri, err := rc.ProxifyURI(attrValue); err == nil {
    794788                        fmt.Fprintf(out, " %s=\"%s\"", attrName, uri)
    795                 } else {
     789                } else if DEBUG {
    796790                        log.Println("cannot proxify uri:", string(attrValue))
    797791                }
     
    943937        _, err := hex.Decode(h, hashMsg)
    944938        if err != nil {
    945                 log.Println("hmac error:", err)
     939                if DEBUG {
     940                        log.Println("hmac error:", err)
     941                }
    946942                return false
    947943        }
     
    969965        ctx.Write([]byte(MORTY_HTML_PAGE_START))
    970966        if err != nil {
    971                 log.Println("error:", err)
     967                if DEBUG {
     968                        log.Println("error:", err)
     969                }
    972970                ctx.Write([]byte("<h2>Error: "))
    973971                ctx.Write([]byte(html.EscapeString(err.Error())))
Note: See TracChangeset for help on using the changeset viewer.