Changeset 44 in code
- Timestamp:
- Nov 28, 2016, 9:59:14 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/morty.go
r43 r44 18 18 "github.com/valyala/fasthttp" 19 19 "golang.org/x/net/html" 20 "golang.org/x/net/html/charset" 21 "golang.org/x/text/encoding" 20 "golang.org/x/text/encoding/charmap" 22 21 ) 23 22 … … 124 123 ` 125 124 126 var HTML_META_CONTENT_TYPE string = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"127 128 125 func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) { 129 126 … … 167 164 req.SetConnectionClose() 168 165 169 reqQuery := parsedURI.Query() 170 ctx.QueryArgs().VisitAll(func(key, value []byte) { 171 reqQuery.Add(string(key), string(value)) 172 }) 173 174 parsedURI.RawQuery = reqQuery.Encode() 175 176 uriStr := parsedURI.String() 177 178 log.Println("getting", uriStr) 179 180 req.SetRequestURI(uriStr) 166 requestURIStr := string(requestURI) 167 168 log.Println("getting", requestURIStr) 169 170 req.SetRequestURI(requestURIStr) 181 171 req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")) 182 172 … … 217 207 } 218 208 } 219 error_message := fmt.Sprintf("invalid response: %d ", resp.StatusCode())209 error_message := fmt.Sprintf("invalid response: %d (%s)", resp.StatusCode(), requestURIStr) 220 210 p.serveMainPage(ctx, resp.StatusCode(), errors.New(error_message)) 221 211 return … … 240 230 var responseBody []byte 241 231 242 if len(contentInfo) == 2 && bytes.Contains(contentInfo[0], []byte("text")) { 243 e, ename, _ := charset.DetermineEncoding(resp.Body(), string(contentType)) 244 if (e != encoding.Nop) && (!strings.EqualFold("utf-8", ename)) { 245 responseBody, err = e.NewDecoder().Bytes(resp.Body()) 246 if err != nil { 247 // HTTP status code 503 : Service Unavailable 248 p.serveMainPage(ctx, 503, err) 249 return 250 } 251 } else { 252 responseBody = resp.Body() 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 253 239 } 254 240 } else { … … 333 319 unsafeElements := make([][]byte, 0, 8) 334 320 state := STATE_DEFAULT 321 335 322 for { 336 323 token := decoder.Next() … … 360 347 for { 361 348 attrName, attrValue, moreAttr := decoder.TagAttr() 362 if bytes.Equal(attrName, []byte("href")) { 363 parsedURI, err := url.Parse(string(attrValue)) 364 if err == nil { 365 rc.BaseURL = parsedURI 366 } 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 367 355 } 368 356 if !moreAttr { … … 395 383 } 396 384 397 if bytes.Equal(tag, []byte("meta")) {398 sanitizeMetaTag(rc, out, attrs)399 break400 }401 402 385 fmt.Fprintf(out, "<%s", tag) 403 386 404 387 if hasAttrs { 405 sanitizeAttrs(rc, out, attrs) 388 if bytes.Equal(tag, []byte("meta")) { 389 sanitizeMetaAttrs(rc, out, attrs) 390 } else { 391 sanitizeAttrs(rc, out, attrs) 392 } 406 393 } 407 394 … … 413 400 state = STATE_IN_STYLE 414 401 } 415 }416 417 if bytes.Equal(tag, []byte("head")) {418 fmt.Fprintf(out, HTML_META_CONTENT_TYPE)419 402 } 420 403 … … 516 499 } 517 500 518 func sanitizeMeta Tag(rc *RequestConfig, out io.Writer, attrs [][][]byte) {501 func sanitizeMetaAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) { 519 502 var http_equiv []byte 520 503 var content []byte … … 529 512 content = attrValue 530 513 } 531 if bytes.Equal(attrName, []byte("charset")) { 532 // exclude <meta charset="..."> 533 return 534 } 535 } 536 537 if bytes.Equal(http_equiv, []byte("content-type")) { 538 return 539 } 540 541 out.Write([]byte("<meta")) 514 } 515 542 516 urlIndex := bytes.Index(bytes.ToLower(content), []byte("url=")) 543 517 if bytes.Equal(http_equiv, []byte("refresh")) && urlIndex != -1 { … … 556 530 sanitizeAttrs(rc, out, attrs) 557 531 } 558 out.Write([]byte(">")) 532 559 533 } 560 534 … … 613 587 return fmt.Sprintf("./?mortyurl=%s", url.QueryEscape(uri)), nil 614 588 } 589 615 590 return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s", hash(uri, rc.Key), url.QueryEscape(uri)), nil 616 591 }
Note:
See TracChangeset
for help on using the changeset viewer.