~sotirisp/kindleto

8726217dd787697d66fcd66131b76b148297a149 — Sotiris Papatheodorou 1 year, 11 months ago 2185b97
Restructure proxy()
1 files changed, 9 insertions(+), 14 deletions(-)

M handlers.go
M handlers.go => handlers.go +9 -14
@@ 191,12 191,8 @@ func certificateChanged(w http.ResponseWriter, r *http.Request) {

// proxy handles requests not covered by another handler.
func proxy(w http.ResponseWriter, r *http.Request) {
	var err error
	var targetURL string
	var u *url.URL

	if r.Method == http.MethodPost && r.FormValue("url") != "" {
		targetURL = strings.SplitN(r.FormValue("url"), "?", 2)[0]
		targetURL := strings.SplitN(r.FormValue("url"), "?", 2)[0]
		if r.FormValue("input") != "" {
			targetURL = targetURL + "?" + gmi.QueryEscape(r.FormValue("input"))
			if settings.Current.LogLevel > 1 {


@@ 210,7 206,6 @@ func proxy(w http.ResponseWriter, r *http.Request) {
		} else {
			targetURL = r.FormValue("url")
		}

		http.Redirect(w, r, "/?url="+gmi.QueryEscape(targetURL), http.StatusFound)
	}



@@ 218,8 213,7 @@ func proxy(w http.ResponseWriter, r *http.Request) {
		if homepage != nil {
			gemini.ProxyGemini(w, r, homepage)
		} else {
			var td templates.TemplateData
			err = templates.Templates.ExecuteTemplate(w, "home.html.tmpl", td)
			err := templates.Templates.ExecuteTemplate(w, "home.html.tmpl", templates.TemplateData{})
			if err != nil {
				log.Println("proxy:", err)
				http.Error(w, "Internal Server Error", 500)


@@ 228,7 222,7 @@ func proxy(w http.ResponseWriter, r *http.Request) {
		return
	}

	u, err = url.Parse(r.URL.Query().Get("url"))
	u, err := url.Parse(r.URL.Query().Get("url"))
	if err != nil {
		err = fmt.Errorf("proxy: failed to parse URL: %v", err)
		log.Println(err)


@@ 265,17 259,18 @@ func proxy(w http.ResponseWriter, r *http.Request) {
			break
		}
	default:
		err = fmt.Errorf("proxy: proxying of %s not supported (%s)", u.Scheme, u.String())
		err = fmt.Errorf("proxy: unsupported URL scheme: %s", u.Scheme)
	}

	if err != nil {
		if settings.Current.LogLevel > 0 {
			log.Println(err)
		}
		var td templates.TemplateData
		td.Error = err.Error()
		td.URL = u.String()
		td.ParentURL = util.ParentURL(*u)
		td := templates.TemplateData{
			Error:     err.Error(),
			URL:       u.String(),
			ParentURL: util.ParentURL(*u),
		}
		err = templates.Templates.ExecuteTemplate(w, "home.html.tmpl", td)
		if err != nil {
			log.Println(err)