<commit_before>package http

import (
	"encoding/json"
	"io"
	"net/http"
	"os"
	"os/exec"
	"strconv"
)

type tailReq struct {
	MaxLineNumber int `json:"MaxLineNumber"`
}

func configTailRoutes() {
	http.HandleFunc("/v1/tail", func(w http.ResponseWriter, req *http.Request) {
		var reqData tailReq
		err := json.NewDecoder(req.Body).Decode(&reqData)
		switch {
		case err == io.EOF:
			// empty body
			reqData.MaxLineNumber = 10
		case err != nil:
			// other error
			http.Error(w, "connot decode body", http.StatusBadRequest)
			return
		}

		pwd, err := os.Getwd()
		if err != nil {
			http.Error(w, "connot get current directory", http.StatusBadRequest)
			return
		}
		filepath := pwd + "/var/app.log"

		cmd := exec.Command("tail", "-n", strconv.Itoa(reqData.MaxLineNumber), filepath)
		out, err := cmd.CombinedOutput()
		if err != nil {
			w.Write([]byte("tail command failed\n"))<commit_msg>[OWL-1267][agent] add more error detection in http request body<commit_after>package http

import (
	"encoding/json"
	"io"
	"net/http"
	"os"
	"os/exec"
	"strconv"
)

type tailReq struct {
	MaxLineNumber int `json:"MaxLineNumber"`
}

func configTailRoutes() {
	http.HandleFunc("/v1/tail", func(w http.ResponseWriter, req *http.Request) {
		var reqData tailReq
		reqData.MaxLineNumber = -1
		err := json.NewDecoder(req.Body).Decode(&reqData)
		switch {
		case err == io.EOF:
			// empty body
			reqData.MaxLineNumber = 10
		case err != nil:
			// parsing error
			http.Error(w, "connot decode body", http.StatusBadRequest)
			return
		}
		if reqData.MaxLineNumber < 0 {
			http.Error(w, "Need positive integer of MaxLineNumber", http.StatusBadRequest)
			return
		}

		pwd, err := os.Getwd()
		if err != nil {
			http.Error(w, "connot get current directory", http.StatusBadRequest)
			return
		}
		filepath := pwd + "/var/app.log"

		cmd := exec.Command("tail", "-n", strconv.Itoa(reqData.MaxLineNumber), filepath)
		out, err := cmd.CombinedOutput()
		if err != nil {
			w.Write([]byte("tail command failed\n"))
{'fraction_non_alphanumeric': 0.14840637450199204, 'fraction_numerical': 0.00597609561752988, 'mean_word_length': 3.650462962962963, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '1974230', 'n_tokens_mistral': 800, 'n_tokens_neox': 721, 'n_words': 172}