<commit_before>// Copyright 2017 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package core

import (
	"net/http"
	"time"

	"github.com/miniflux/miniflux2/server/template"
)

// Response handles HTTP responses.
type Response struct {
	writer   http.ResponseWriter
	request  *http.Request
	template *template.Engine
}

// SetCookie send a cookie to the client.
func (r *Response) SetCookie(cookie *http.Cookie) {
	http.SetCookie(r.writer, cookie)
}

// JSON returns a JSONResponse.
func (r *Response) JSON() *JSONResponse {
	r.commonHeaders()
	return &JSONResponse{writer: r.writer, request: r.request}
}

// HTML returns a HTMLResponse.
func (r *Response) HTML() *HTMLResponse {
	r.commonHeaders()
	return &HTMLResponse{writer: r.writer, request: r.request, template: r.template}
}

// XML returns a XMLResponse.
func (r *Response) XML() *XMLResponse {
	r.commonHeaders()
	return &XMLResponse{writer: r.writer, request: r.request}
}

// Redirect redirects the user to another location.
func (r *Response) Redirect(path string) {
	http.Redirect(r.writer, r.request, path, http.StatusFound)
}

// NotModified sends a response with a 304 status code.
func (r *Response) NotModified() {
	r.writer.WriteHeader(http.StatusNotModified)
}

// Cache returns a response with caching headers.
func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
	r.writer.Header().Set("Content-Type", mimeType)
	r.writer.Header().Set("Etag", etag)
	r.writer.Header().Set("Cache-Control", "public")
	r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))

	if etag == r.request.Header.Get("If-None-Match") {
		r.writer.WriteHeader(http.StatusNotModified)
	} else {
		r.writer.Write(content)
	}
}

func (r *Response) commonHeaders() {
	r.writer.Header().Set("X-XSS-Protection", "1; mode=block")
	r.writer.Header().Set("X-Content-Type-Options", "nosniff")
	r.writer.Header().Set("X-Frame-Options", "DENY")
}

// NewResponse returns a new Response.
func NewResponse(w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
	return &Response{writer: w, request: r, template: template}
}
<commit_msg>Add Content-Security-Policy header<commit_after>// Copyright 2017 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package core

import (
	"net/http"
	"time"

	"github.com/miniflux/miniflux2/server/template"
)

// Response handles HTTP responses.
type Response struct {
	writer   http.ResponseWriter
	request  *http.Request
	template *template.Engine
}

// SetCookie send a cookie to the client.
func (r *Response) SetCookie(cookie *http.Cookie) {
	http.SetCookie(r.writer, cookie)
}

// JSON returns a JSONResponse.
func (r *Response) JSON() *JSONResponse {
	r.commonHeaders()
	return &JSONResponse{writer: r.writer, request: r.request}
}

// HTML returns a HTMLResponse.
func (r *Response) HTML() *HTMLResponse {
	r.commonHeaders()
	return &HTMLResponse{writer: r.writer, request: r.request, template: r.template}
}

// XML returns a XMLResponse.
func (r *Response) XML() *XMLResponse {
	r.commonHeaders()
	return &XMLResponse{writer: r.writer, request: r.request}
}

// Redirect redirects the user to another location.
func (r *Response) Redirect(path string) {
	http.Redirect(r.writer, r.request, path, http.StatusFound)
}

// NotModified sends a response with a 304 status code.
func (r *Response) NotModified() {
	r.writer.WriteHeader(http.StatusNotModified)
}

// Cache returns a response with caching headers.
func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
	r.writer.Header().Set("Content-Type", mimeType)
	r.writer.Header().Set("Etag", etag)
	r.writer.Header().Set("Cache-Control", "public")
	r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))

	if etag == r.request.Header.Get("If-None-Match") {
		r.writer.WriteHeader(http.StatusNotModified)
	} else {
		r.writer.Write(content)
	}
}

func (r *Response) commonHeaders() {
	r.writer.Header().Set("X-XSS-Protection", "1; mode=block")
	r.writer.Header().Set("X-Content-Type-Options", "nosniff")
	r.writer.Header().Set("X-Frame-Options", "DENY")
	r.writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src *; media-src *; frame-src *")
}

// NewResponse returns a new Response.
func NewResponse(w http.ResponseWriter, r *http.Request, template *template.Engine) *Response {
	return &Response{writer: w, request: r, template: template}
}

{'fraction_non_alphanumeric': 0.1482444733420026, 'fraction_numerical': 0.006501950585175552, 'mean_word_length': 4.628048780487805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6165561', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1504, 'n_words': 406}