Changeset 23 in code


Ignore:
Timestamp:
Oct 30, 2016, 4:54:54 PM (9 years ago)
Author:
asciimoo
Message:

[mod] proxification refactor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r21 r23  
    104104type RequestConfig struct {
    105105        Key     []byte
    106         baseURL *url.URL
     106        BaseURL *url.URL
    107107}
    108108
     
    197197                        loc := resp.Header.Peek("Location")
    198198                        if loc != nil {
    199                                 url, err := proxifyURI(&RequestConfig{p.Key, parsedURI}, string(loc))
     199                                rc := &RequestConfig{Key: p.Key, BaseURL: parsedURI}
     200                                url, err := rc.ProxifyURI(string(loc))
    200201                                if err == nil {
    201202                                        ctx.SetStatusCode(resp.StatusCode())
     
    241242        switch {
    242243        case bytes.Contains(contentType, []byte("css")):
    243                 sanitizeCSS(&RequestConfig{p.Key, parsedURI}, ctx, responseBody)
     244                sanitizeCSS(&RequestConfig{Key: p.Key, BaseURL: parsedURI}, ctx, responseBody)
    244245        case bytes.Contains(contentType, []byte("html")):
    245                 sanitizeHTML(&RequestConfig{p.Key, parsedURI}, ctx, responseBody)
     246                sanitizeHTML(&RequestConfig{Key: p.Key, BaseURL: parsedURI}, ctx, responseBody)
    246247        default:
    247248                ctx.Write(responseBody)
     
    291292                urlEnd := s[5]
    292293
    293                 if uri, err := proxifyURI(rc, string(css[urlStart:urlEnd])); err == nil {
     294                if uri, err := rc.ProxifyURI(string(css[urlStart:urlEnd])); err == nil {
    294295                        out.Write(css[startIndex:urlStart])
    295296                        out.Write([]byte(uri))
     
    383384                                                if bytes.Equal(attr[0], []byte("action")) {
    384385                                                        formURL, _ = url.Parse(string(attr[1]))
    385                                                         mergeURIs(rc.baseURL, formURL)
     386                                                        mergeURIs(rc.BaseURL, formURL)
    386387                                                        break
    387388                                                }
    388389                                        }
    389390                                        if formURL == nil {
    390                                                 formURL = rc.baseURL
     391                                                formURL = rc.BaseURL
    391392                                        }
    392393                                        urlStr := formURL.String()
     
    404405                                switch string(tag) {
    405406                                case "body":
    406                                         fmt.Fprintf(out, HTML_BODY_EXTENSION, rc.baseURL.String())
     407                                        fmt.Fprintf(out, HTML_BODY_EXTENSION, rc.BaseURL.String())
    407408                                case "style":
    408409                                        state = STATE_DEFAULT
     
    493494        if bytes.Equal(http_equiv, []byte("refresh")) && urlIndex != -1 {
    494495                contentUrl := content[urlIndex+4:]
    495                 if uri, err := proxifyURI(rc, string(contentUrl)); err == nil {
     496                if uri, err := rc.ProxifyURI(string(contentUrl)); err == nil {
    496497                        fmt.Fprintf(out, ` http-equiv="refresh" content="%surl=%s"`, content[:urlIndex], uri)
    497498                }
     
    515516        switch string(attrName) {
    516517        case "src", "href", "action":
    517                 if uri, err := proxifyURI(rc, string(attrValue)); err == nil {
     518                if uri, err := rc.ProxifyURI(string(attrValue)); err == nil {
    518519                        fmt.Fprintf(out, " %s=\"%s\"", attrName, uri)
    519520                } else {
     
    539540}
    540541
    541 func proxifyURI(rc *RequestConfig, uri string) (string, error) {
     542func (rc *RequestConfig) ProxifyURI(uri string) (string, error) {
    542543        // TODO check malicious data: - e.g. data:script
    543544        if strings.HasPrefix(uri, "data:") {
     
    553554                return "", err
    554555        }
    555         mergeURIs(rc.baseURL, u)
     556        mergeURIs(rc.BaseURL, u)
    556557
    557558        uri = u.String()
Note: See TracChangeset for help on using the changeset viewer.