Example #1
0
func (j *jsminTransform) Transform(m Module) (Module, error) {
	if m.Ext() != jsExt {
		return m, nil
	}

	content, err := m.Content()
	if err != nil {
		return nil, err
	}

	out := new(bytes.Buffer)
	jsmin.Run(bytes.NewBuffer(content), out)
	return NewScriptModule(m.Name(), out.Bytes()), nil
}
Example #2
0
func minify(infile, outfile string) error {
	in, err := os.Open(infile)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(outfile)
	if err != nil {
		return err
	}
	defer out.Close()

	jsmin.Run(in, out)
	return nil
}
Example #3
0
func (state *measurementsServerState) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	requestCount.Inc(1)
	log.Printf("serving %v", r.URL)

	substrate := parseContentType(r.URL.Path)
	switch substrate {
	case HtmlExtension:
		w.Header().Set("Content-Type", "text/html")
	case JavaScriptExtension:
		w.Header().Set("Content-Type", "application/javascript")
	}
	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
	w.Header().Set("Pragma", "no-cache")

	hints := parseHints(r)

	clientIp := r.Header.Get("X-Real-Ip")
	if clientIp == "" {
		clientIp = r.RemoteAddr
	}
	hints["country"], _ = state.Geolocator.GetCountry(clientIp)

	if disabled, ok := hints["disable"]; ok && disabled == "true" {
		log.Printf("user opted out of Encore")
		w.WriteHeader(http.StatusOK)
		optOutCount.Inc(1)
		return
	}

	// Select a task template
	task := state.selectTask(hints)
	if task == nil {
		log.Printf("cannot find viable task")
		w.WriteHeader(http.StatusInternalServerError)
		noViableTaskCount.Inc(1)
		return
	}

	taskType, ok := task.Parameters["taskType"]
	if !ok || !taskType.Valid {
		log.Printf("malformed task: missing taskType")
		w.WriteHeader(http.StatusInternalServerError)
		missingTaskTypeCount.Inc(1)
		return
	}

	templateName := taskType.String + substrate

	// Select task parameters
	taskParameters := make(map[string]string)
	taskParameters["serverUrl"] = state.ServerUrl
	taskParameters["measurementId"] = <-state.MeasurementIds
	taskParameters["hintJQueryAlreadyLoaded"] = hints["jQueryAlreadyLoaded"]
	taskParameters["hintShowStats"] = hints["showStats"]
	taskParameters["hintCountry"] = hints["country"]
	if showStats, ok := hints["showStats"]; !ok || showStats != "false" {
		count, err := countResultsForReferer(state.CountResultsRequests, r)
		if err != nil {
			log.Printf("error counting results: %v", err)
			countResultsErrorCount.Inc(1)
		}
		taskParameters["count"] = fmt.Sprint(count)
	}
	for k, v := range task.Parameters {
		if !v.Valid {
			continue
		}
		taskParameters[k] = v.String
	}

	// Execute the template
	responseBody := bytes.Buffer{}
	if err := state.Templates.ExecuteTemplate(&responseBody, templateName, taskParameters); err != nil {
		log.Printf("error executing task template %s: %v", templateName, err)
		w.WriteHeader(http.StatusInternalServerError)
		templateExecutionErrorCount.Inc(1)
		return
	}

	if minify, ok := hints["minify"]; ok && minify == "false" {
		responseBody.WriteTo(w)
		minifiedCount.Inc(1)
	} else {
		jsmin.Run(&responseBody, w)
		unminifiedCount.Inc(1)
	}

	var rawRequest bytes.Buffer
	if err := r.Write(&rawRequest); err != nil {
		log.Print("error writing HTTP request")
		w.WriteHeader(http.StatusInternalServerError)
		requestParseErrorCount.Inc(1)
		return
	}
	parametersBytes, err := json.Marshal(taskParameters)
	if err != nil {
		log.Printf("cannot marshal task parameters to JSON")
		w.WriteHeader(http.StatusInternalServerError)
		parametersMarshalErrorCount.Inc(1)
		return
	}

	state.Queries <- &store.Query{
		Timestamp:      time.Now(),
		RemoteAddr:     r.RemoteAddr,
		RawRequest:     rawRequest.Bytes(),
		Task:           task.Id,
		Substrate:      substrate,
		ParametersJson: parametersBytes,
		ResponseBody:   responseBody.Bytes(),
	}

	responseCount.Inc(1)
}