Changeset 45 in code for trunk/morty.go
- Timestamp:
- Nov 28, 2016, 12:49:22 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/morty.go
r44 r45 18 18 "github.com/valyala/fasthttp" 19 19 "golang.org/x/net/html" 20 "golang.org/x/text/encoding/charmap" 20 "golang.org/x/net/html/charset" 21 "golang.org/x/text/encoding" 21 22 ) 22 23 … … 123 124 ` 124 125 126 var HTML_META_CONTENT_TYPE string = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" 127 125 128 func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { 126 129 … … 230 233 var responseBody []byte 231 234 232 if len(contentInfo) == 2 && bytes.Contains(contentInfo[1], []byte("ISO-8859-2")) && bytes.Contains(contentInfo[0], []byte("text")) { 233 var err error 234 responseBody, err = charmap.ISO8859_2.NewDecoder().Bytes(resp.Body()) 235 if err != nil { 236 // HTTP status code 503 : Service Unavailable 237 p.serveMainPage(ctx, 503, err) 238 return 235 if len(contentInfo) == 2 && bytes.Contains(contentInfo[0], []byte("text")) { 236 e, ename, _ := charset.DetermineEncoding(resp.Body(), string(contentType)) 237 if (e != encoding.Nop) && (!strings.EqualFold("utf-8", ename)) { 238 responseBody, err = e.NewDecoder().Bytes(resp.Body()) 239 if err != nil { 240 // HTTP status code 503 : Service Unavailable 241 p.serveMainPage(ctx, 503, err) 242 return 243 } 244 } else { 245 responseBody = resp.Body() 239 246 } 240 247 } else { … … 319 326 unsafeElements := make([][]byte, 0, 8) 320 327 state := STATE_DEFAULT 321 322 328 for { 323 329 token := decoder.Next() … … 347 353 for { 348 354 attrName, attrValue, moreAttr := decoder.TagAttr() 349 if !bytes.Equal(attrName, []byte("href")) { 350 continue 351 } 352 parsedURI, err := url.Parse(string(attrValue)) 353 if err == nil { 354 rc.BaseURL = parsedURI 355 if bytes.Equal(attrName, []byte("href")) { 356 parsedURI, err := url.Parse(string(attrValue)) 357 if err == nil { 358 rc.BaseURL = parsedURI 359 } 355 360 } 356 361 if !moreAttr { … … 383 388 } 384 389 390 if bytes.Equal(tag, []byte("meta")) { 391 sanitizeMetaTag(rc, out, attrs) 392 break 393 } 394 385 395 fmt.Fprintf(out, "<%s", tag) 386 396 387 397 if hasAttrs { 388 if bytes.Equal(tag, []byte("meta")) { 389 sanitizeMetaAttrs(rc, out, attrs) 390 } else { 391 sanitizeAttrs(rc, out, attrs) 392 } 398 sanitizeAttrs(rc, out, attrs) 393 399 } 394 400 … … 400 406 state = STATE_IN_STYLE 401 407 } 408 } 409 410 if bytes.Equal(tag, []byte("head")) { 411 fmt.Fprintf(out, HTML_META_CONTENT_TYPE) 402 412 } 403 413 … … 499 509 } 500 510 501 func sanitizeMeta Attrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {511 func sanitizeMetaTag(rc *RequestConfig, out io.Writer, attrs [][][]byte) { 502 512 var http_equiv []byte 503 513 var content []byte … … 512 522 content = attrValue 513 523 } 514 } 515 524 if bytes.Equal(attrName, []byte("charset")) { 525 // exclude <meta charset="..."> 526 return 527 } 528 } 529 530 if bytes.Equal(http_equiv, []byte("content-type")) { 531 return 532 } 533 534 out.Write([]byte("<meta")) 516 535 urlIndex := bytes.Index(bytes.ToLower(content), []byte("url=")) 517 536 if bytes.Equal(http_equiv, []byte("refresh")) && urlIndex != -1 { … … 530 549 sanitizeAttrs(rc, out, attrs) 531 550 } 532 551 out.Write([]byte(">")) 533 552 } 534 553
Note:
See TracChangeset
for help on using the changeset viewer.