github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/repo/9_html_form_fetch_similar.go (about)

     1  package repo
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"html"
     8  	"net/http"
     9  
    10  	"github.com/pbberlin/tools/appengine/util_appengine"
    11  	"github.com/pbberlin/tools/net/http/fetch"
    12  	"github.com/pbberlin/tools/net/http/loghttp"
    13  	"github.com/pbberlin/tools/net/http/routes"
    14  	"github.com/pbberlin/tools/net/http/tplx"
    15  
    16  	tt "html/template"
    17  )
    18  
    19  const htmlForm = `
    20  	<style> .ib { display:inline-block; }</style>
    21  
    22  
    23  
    24  	<form>
    25  		<div style='margin:8px;'>
    26  			<span class='ib' style='width:50px'>Count </span>
    27  			<input id='inp1' name="cnt"                      size="3"  value="2"><br/>
    28  
    29  			<span class='ib' style='width:50px'>Protocol </span>
    30  			<input id='inp1' name="prot"                      size="6"  value="http"><br/>
    31  
    32  			<span class='ib' style='width:50px'>URL </span>
    33  			<input id='inp2' name="{{.fieldname}}"           size="120"  value="{{.val}}"><br/>
    34  			
    35  			<span class='ib' style='width:50px' ></span> 
    36  			<span class='ib' tabindex='11'> 
    37  			www.welt.de/politik/ausland/article146154432/Tuerkische-Bodentruppen-marschieren-im-Nordirak-ein.html
    38  			</span>
    39  			<br/>
    40  
    41  			<span class='ib' style='width:50px' ></span> 
    42  			<span class='ib' tabindex='11'> 
    43  			www.economist.com/news/britain/21663648-hard-times-hard-hats-making-britain-make-things-again-proving-difficult  
    44  			</span>
    45  			<br/>
    46  
    47  			<span class='ib' style='width:50px' ></span> 
    48  			<span class='ib' tabindex='12'> 
    49  			www.economist.com/news/americas/21661804-gender-equality-good-economic-growth-girl-power  
    50  			</span>
    51  			<br/>
    52  
    53  			<span class='ib' style='width:50px'> </span>
    54  			<input type="submit" value="Get similar (shit+alt+f)" accesskey='f'>
    55  		</div>
    56  	</form>
    57  
    58  	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" 
    59  			type="text/javascript"></script>
    60  
    61  
    62  	<script>
    63  		var focus = 0,
    64  		blur = 0;
    65  		//focusout
    66  		$( "span" ).focusin(function() {
    67  			focus++;
    68  			//$( "#inp2" ).text( "focusout fired: " + focus + "x" );
    69  			$( "#inp2" ).val(  $.trim( $(this).text() )   );
    70  			console.log("fired")
    71  		});
    72  	</script>	
    73  
    74  
    75  
    76  	`
    77  
    78  func fetchSimForm(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    79  
    80  	lg, b := loghttp.BuffLoggerUniversal(w, r)
    81  	closureOverBuf := func(bUnused *bytes.Buffer) {
    82  		loghttp.Pf(w, r, b.String())
    83  	}
    84  	defer closureOverBuf(b) // the argument is ignored,
    85  
    86  	r.Header.Set("X-Custom-Header-Counter", "nocounter")
    87  
    88  	// on live server => always use https
    89  	if r.URL.Scheme != "https" && !util_appengine.IsLocalEnviron() {
    90  		r.URL.Scheme = "https"
    91  		r.URL.Host = r.Host
    92  		lg("lo - redirect %v", r.URL.String())
    93  		http.Redirect(w, r, r.URL.String(), http.StatusFound)
    94  	}
    95  
    96  	err := r.ParseForm()
    97  	lg(err)
    98  
    99  	rURL := ""
   100  	if r.FormValue(routes.URLParamKey) != "" {
   101  		rURL = r.FormValue(routes.URLParamKey)
   102  	}
   103  	if len(rURL) == 0 {
   104  
   105  		wpf(b, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Find similar HTML URLs"}))
   106  		defer wpf(b, tplx.Foot)
   107  
   108  		tm := map[string]string{
   109  			"val":       "www.welt.de/politik/ausland/article146154432/Tuerkische-Bodentruppen-marschieren-im-Nordirak-ein.html",
   110  			"fieldname": routes.URLParamKey,
   111  		}
   112  		tplForm := tt.Must(tt.New("tplName01").Parse(htmlForm))
   113  		tplForm.Execute(b, tm)
   114  
   115  	} else {
   116  
   117  		fullURL := fmt.Sprintf("https://%s%s?%s=%s&cnt=%s&prot=%s", r.Host, routes.FetchSimilarURI,
   118  			routes.URLParamKey, rURL, r.FormValue("cnt"), r.FormValue("prot"))
   119  		lg("lo - sending to URL 1: %v", fullURL)
   120  
   121  		fo := fetch.Options{}
   122  		fo.URL = fullURL
   123  		bts, inf, err := fetch.UrlGetter(r, fo)
   124  		_ = inf
   125  		lg(err)
   126  		if err != nil {
   127  			return
   128  		}
   129  
   130  		if len(bts) == 0 {
   131  			lg("empty bts")
   132  			return
   133  		}
   134  
   135  		var mp map[string][]byte
   136  		err = json.Unmarshal(bts, &mp)
   137  		lg(err)
   138  		if err != nil {
   139  			lg("%s", bts)
   140  			return
   141  		}
   142  
   143  		w.Header().Set("Content-Type", "text/html; charset=utf-8")
   144  		if _, ok := mp["msg"]; ok {
   145  			w.Write(mp["msg"])
   146  		}
   147  
   148  		for k, v := range mp {
   149  			if k != "msg" {
   150  				wpf(w, "<br><br>%s:\n", k)
   151  				if true {
   152  					wpf(w, "len %v", len(v))
   153  				} else {
   154  					wpf(w, "%s", html.EscapeString(string(v)))
   155  				}
   156  			}
   157  		}
   158  
   159  	}
   160  
   161  }