<commit_before>package formutil

import (
	"encoding/json"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/http"
	"net/url"
	"strings"
)

// Parse Url Query string
func ParseUrlQuery(r *http.Request) {
	Form, err := url.ParseQuery(r.URL.RawQuery)
	if err != nil {
		return
	}
	r.Form = Form
}

// Parse Body of Request
func ParseBody(r *http.Request) {
	if r.PostForm != nil {
		return
	}

	r.PostForm = url.Values{}

	if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return
	}
	r.Body.Close()

	PostForm, err := url.ParseQuery(string(body))
	if err != nil {
		return
	}
	r.PostForm = PostForm
}

// Parse Body of Request (Multipart)
func ParseMultipartBody(r *http.Request, maxMemory int64) {
	if r.MultipartForm != nil {
		return
	}

	r.PostForm = url.Values{}

	mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
	if err != nil || !strings.HasPrefix(mediaType, "multipart/") {
		return
	}

	mr := multipart.NewReader(r.Body, params["boundary"])
	defer r.Body.Close()

	MultipartForm, err := mr.ReadForm(maxMemory)
	if err != nil {
		return
	}

	r.MultipartForm = MultipartForm
}

// Parse http://api.jquery.com/serializearray/ in JSON format
// Request Body must be in JSON format. 'JSON.stringify(object);' in Javascript.
// Eg [{"name":"","value":""},{"name":"","value":""},{"name":"","value":""}...]<commit_msg>Added notice<commit_after>package formutil

import (
	"encoding/json"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/http"
	"net/url"
	"strings"
)

// Parse Url Query string
func ParseUrlQuery(r *http.Request) {
	Form, err := url.ParseQuery(r.URL.RawQuery)
	if err != nil {
		return
	}
	r.Form = Form
}

// Parse Body of Request
func ParseBody(r *http.Request) {
	if r.PostForm != nil {
		return
	}

	r.PostForm = url.Values{}

	if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return
	}
	r.Body.Close()

	PostForm, err := url.ParseQuery(string(body))
	if err != nil {
		return
	}
	r.PostForm = PostForm
}

// Parse Body of Request (Multipart)
func ParseMultipartBody(r *http.Request, maxMemory int64) {
	if r.MultipartForm != nil {
		return
	}

	r.PostForm = url.Values{}

	mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
	if err != nil || !strings.HasPrefix(mediaType, "multipart/") {
		return
	}

	mr := multipart.NewReader(r.Body, params["boundary"])
	defer r.Body.Close()

	MultipartForm, err := mr.ReadForm(maxMemory)
	if err != nil {
		return
	}

	r.MultipartForm = MultipartForm
}

// Parse http://api.jquery.com/serializearray/ in JSON format
// Request Body must be in JSON format. 'JSON.stringify(object);' in Javascript.
// Eg [{"name":"","value":""},{"name":"","value":""},{"name":"","value":""}...]
//
// Notice both 'name' and 'value' must be string or it won't work.
{'fraction_non_alphanumeric': 0.17581677332435164, 'fraction_numerical': 0.0013472549680026945, 'mean_word_length': 3.7980613893376414, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5731886', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1097, 'n_words': 246}