API filtering/add_url -- accept JSON instead of name=value lines
This commit is contained in:
parent
33958c5a25
commit
751be05a31
31
control.go
31
control.go
|
@ -437,6 +437,7 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func httpError(w http.ResponseWriter, code int, format string, args ...interface{}) {
|
func httpError(w http.ResponseWriter, code int, format string, args ...interface{}) {
|
||||||
text := fmt.Sprintf(format, args...)
|
text := fmt.Sprintf(format, args...)
|
||||||
|
log.Println(text)
|
||||||
http.Error(w, text, code)
|
http.Error(w, text, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -705,55 +706,49 @@ func handleFilteringStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
|
func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
|
||||||
parameters, err := parseParametersFromBody(r.Body)
|
filter := filter{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errortext := fmt.Sprintf("failed to parse parameters from body: %s", err)
|
httpError(w, http.StatusBadRequest, "Failed to parse request body json: %s", err)
|
||||||
log.Println(errortext)
|
|
||||||
http.Error(w, errortext, 400)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
url, ok := parameters["url"]
|
filter.Enabled = true
|
||||||
if !ok {
|
if len(filter.URL) == 0 {
|
||||||
http.Error(w, "URL parameter was not specified", 400)
|
http.Error(w, "URL parameter was not specified", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if valid := govalidator.IsRequestURL(url); !valid {
|
if valid := govalidator.IsRequestURL(filter.URL); !valid {
|
||||||
http.Error(w, "URL parameter is not valid request URL", 400)
|
http.Error(w, "URL parameter is not valid request URL", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for duplicates
|
// check for duplicates
|
||||||
for i := range config.Filters {
|
for i := range config.Filters {
|
||||||
filter := &config.Filters[i]
|
if config.Filters[i].URL == filter.URL {
|
||||||
if filter.URL == url {
|
errortext := fmt.Sprintf("Filter URL already added -- %s", filter.URL)
|
||||||
errortext := fmt.Sprintf("Filter URL already added -- %s", url)
|
|
||||||
log.Println(errortext)
|
log.Println(errortext)
|
||||||
http.Error(w, errortext, http.StatusBadRequest)
|
http.Error(w, errortext, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var filter = filter{
|
ok, err := filter.update(time.Now())
|
||||||
Enabled: true,
|
|
||||||
URL: url,
|
|
||||||
}
|
|
||||||
ok, err = filter.update(time.Now())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errortext := fmt.Sprintf("Couldn't fetch filter from url %s: %s", url, err)
|
errortext := fmt.Sprintf("Couldn't fetch filter from url %s: %s", filter.URL, err)
|
||||||
log.Println(errortext)
|
log.Println(errortext)
|
||||||
http.Error(w, errortext, http.StatusBadRequest)
|
http.Error(w, errortext, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if filter.RulesCount == 0 {
|
if filter.RulesCount == 0 {
|
||||||
errortext := fmt.Sprintf("Filter at url %s has no rules (maybe it points to blank page?)", url)
|
errortext := fmt.Sprintf("Filter at url %s has no rules (maybe it points to blank page?)", filter.URL)
|
||||||
log.Println(errortext)
|
log.Println(errortext)
|
||||||
http.Error(w, errortext, http.StatusBadRequest)
|
http.Error(w, errortext, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
errortext := fmt.Sprintf("Filter at url %s is invalid (maybe it points to blank page?)", url)
|
errortext := fmt.Sprintf("Filter at url %s is invalid (maybe it points to blank page?)", filter.URL)
|
||||||
log.Println(errortext)
|
log.Println(errortext)
|
||||||
http.Error(w, errortext, http.StatusBadRequest)
|
http.Error(w, errortext, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue