| 48 | } |
| 49 | |
| 50 | func (s *Source) processRequest(w http.ResponseWriter, r *http.Request, hc *Configuration, out chan pipeline.Event) error { |
| 51 | if hc.MaxBodySize != nil && r.ContentLength > *hc.MaxBodySize { |
| 52 | w.WriteHeader(http.StatusRequestEntityTooLarge) |
| 53 | return fmt.Errorf("body size exceeds max body size: %d > %d", r.ContentLength, *hc.MaxBodySize) |
| 54 | } |
| 55 | |
| 56 | srcHost, _, err := net.SplitHostPort(r.RemoteAddr) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | defer r.Body.Close() |
| 62 | |
| 63 | if s.logger.Logger.IsLevelEnabled(log.TraceLevel) { |
| 64 | s.logger.Tracef("processing request from '%s' with method '%s' and path '%s'", r.RemoteAddr, r.Method, r.URL.Path) |
| 65 | |
| 66 | bodyContent, err := httputil.DumpRequest(r, true) |
| 67 | if err != nil { |
| 68 | s.logger.Errorf("failed to dump request: %s", err) |
| 69 | } else { |
| 70 | s.logger.Tracef("request body: %s", bodyContent) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | reader := r.Body |
| 75 | |
| 76 | if r.Header.Get("Content-Encoding") == "gzip" { |
| 77 | reader, err = gzip.NewReader(r.Body) |
| 78 | if err != nil { |
| 79 | w.WriteHeader(http.StatusBadRequest) |
| 80 | return fmt.Errorf("failed to create gzip reader: %w", err) |
| 81 | } |
| 82 | defer reader.Close() |
| 83 | } |
| 84 | |
| 85 | decoder := json.NewDecoder(reader) |
| 86 | |
| 87 | for { |
| 88 | var message json.RawMessage |
| 89 | |
| 90 | if err := decoder.Decode(&message); err != nil { |
| 91 | if err == io.EOF { |
| 92 | break |
| 93 | } |
| 94 | |
| 95 | w.WriteHeader(http.StatusBadRequest) |
| 96 | |
| 97 | return fmt.Errorf("failed to decode: %w", err) |
| 98 | } |
| 99 | |
| 100 | line := pipeline.Line{ |
| 101 | Raw: string(message), |
| 102 | Src: srcHost, |
| 103 | Time: time.Now().UTC(), |
| 104 | Labels: hc.Labels, |
| 105 | Process: true, |
| 106 | Module: s.GetName(), |
| 107 | } |