Changeset 11 in code for trunk/morty.go
- Timestamp:
- Oct 25, 2016, 9:01:01 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/morty.go
r10 r11 134 134 135 135 if requestURI == nil { 136 p. breakOnError(ctx, errors.New(`missing "mortyurl" URL parameter`))136 p.serveMainPage(ctx, nil) 137 137 return 138 138 } … … 140 140 if p.Key != nil { 141 141 if !verifyRequestURI(requestURI, requestHash, p.Key) { 142 p. breakOnError(ctx, errors.New("invalid hash"))142 p.serveMainPage(ctx, errors.New(`invalid "mortyhash" parameter`)) 143 143 return 144 144 } … … 147 147 parsedURI, err := url.Parse(string(requestURI)) 148 148 149 if p.breakOnError(ctx, err) { 149 if err != nil { 150 p.serveMainPage(ctx, err) 150 151 return 151 152 } … … 176 177 } 177 178 178 if p.breakOnError(ctx, CLIENT.DoTimeout(req, resp, p.RequestTimeout)) { 179 err = CLIENT.DoTimeout(req, resp, p.RequestTimeout) 180 181 if err != nil { 182 p.serveMainPage(ctx, err) 179 183 return 180 184 } … … 201 205 202 206 if contentType == nil { 203 p. breakOnError(ctx, errors.New("invalid content type"))207 p.serveMainPage(ctx, errors.New("invalid content type")) 204 208 return 205 209 } … … 212 216 var err error 213 217 responseBody, err = charmap.ISO8859_2.NewDecoder().Bytes(resp.Body()) 214 if p.breakOnError(ctx, err) { 218 if err != nil { 219 p.serveMainPage(ctx, err) 215 220 return 216 221 } … … 232 237 233 238 func appRequestHandler(ctx *fasthttp.RequestCtx) bool { 239 // serve robots.txt 234 240 if bytes.Equal(ctx.Path(), []byte("/robots.txt")) { 235 241 ctx.SetContentType("text/plain") … … 237 243 return true 238 244 } 245 239 246 return false 240 247 } … … 527 534 } 528 535 529 func (p *Proxy) breakOnError(ctx *fasthttp.RequestCtx, err error) bool { 530 if err == nil { 531 return false 532 } 533 log.Println("error:", err) 534 ctx.SetStatusCode(404) 536 func (p *Proxy) serveMainPage(ctx *fasthttp.RequestCtx, err error) { 535 537 ctx.SetContentType("text/html") 536 538 ctx.Write([]byte(`<!doctype html> 537 539 <head> 538 <title>MortyError</title> 540 <title>MortyProxy</title> 541 <style> 542 body { font-family: 'Garamond', 'Georgia', serif; text-align: center; color: #444; background: #FAFAFA; margin: 0; padding: 0; font-size: 1.1em; } 543 input { border: 1px solid #888; padding: 0.3em; color: #444; background: #FFF; font-size: 1.1em; } 544 a { text-decoration: none; #2980b9; } 545 h1, h2 { font-weight: 200; margin-bottom: 2rem; } 546 h1 { font-size: 3em; } 547 .footer { position: absolute; bottom: 2em; width: 100%; } 548 .footer p { font-size: 0.8em; } 549 550 </style> 539 551 </head> 540 <body><h2>Error!</h2>`)) 541 ctx.Write([]byte("<h3>")) 542 ctx.Write([]byte(html.EscapeString(err.Error()))) 543 ctx.Write([]byte("</h3>")) 552 <body> 553 <h1>MortyProxy</h1>`)) 554 if err != nil { 555 ctx.SetStatusCode(404) 556 log.Println("error:", err) 557 ctx.Write([]byte("<h2>Error: ")) 558 ctx.Write([]byte(html.EscapeString(err.Error()))) 559 ctx.Write([]byte("</h2>")) 560 } else { 561 ctx.SetStatusCode(200) 562 } 544 563 if p.Key == nil { 545 564 ctx.Write([]byte(` … … 548 567 <input type="submit" value="go" /> 549 568 </form>`)) 569 } else { 570 ctx.Write([]byte(`<h3>Warning! This instance does not support direct URL opening.</h3>`)) 550 571 } 551 572 ctx.Write([]byte(` 573 <div class="footer"> 574 <p>Morty rewrites web pages to exclude malicious HTML tags and CSS/HTML attributes. It also replaces external resource references to prevent third-party information leaks.<br /> 575 <a href="https://github.com/asciimoo/morty">view on github</a> 576 </p> 577 </div> 552 578 </body> 553 579 </html>`)) 554 return true555 580 } 556 581
Note:
See TracChangeset
for help on using the changeset viewer.