MCPcopy
hub / github.com/safing/portmaster / ServeHTTP

Method ServeHTTP

service/netquery/query_handler.go:126–216  ·  view source on GitHub ↗
(resp http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

124}
125
126func (batch *BatchQueryHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
127 timing := servertiming.FromContext(req.Context())
128
129 timingQueryParsed := timing.NewMetric("query_parsed").
130 WithDesc("Query has been parsed").
131 Start()
132
133 requestPayload, err := parseQueryRequestPayload[BatchQueryRequestPayload](req)
134 if err != nil {
135 http.Error(resp, err.Error(), http.StatusBadRequest)
136
137 return
138 }
139
140 timingQueryParsed.Stop()
141
142 response := make(map[string][]map[string]any, len(*requestPayload))
143
144 batches := make([]BatchExecute, 0, len(*requestPayload))
145
146 for key, query := range *requestPayload {
147
148 timingQueryBuilt := timing.NewMetric("query_built_" + key).
149 WithDesc("The SQL query has been built").
150 Start()
151
152 sql, paramMap, err := query.generateSQL(req.Context(), batch.Database.Schema)
153 if err != nil {
154 http.Error(resp, err.Error(), http.StatusBadRequest)
155
156 return
157 }
158
159 timingQueryBuilt.Stop()
160
161 var result []map[string]any
162 batches = append(batches, BatchExecute{
163 ID: key,
164 SQL: sql,
165 Params: paramMap,
166 Result: &result,
167 })
168 }
169
170 timingQueryExecute := timing.NewMetric("sql_exec").
171 WithDesc("SQL query execution time").
172 Start()
173
174 status := http.StatusOK
175 if err := batch.Database.ExecuteBatch(req.Context(), batches); err != nil {
176 status = http.StatusInternalServerError
177
178 var merr *multierror.Error
179 if errors.As(err, &merr) {
180 for _, e := range merr.Errors {
181 resp.Header().Add("X-Query-Error", e.Error())
182 }
183 } else {

Callers

nothing calls this directly

Calls 10

ErrorfFunction · 0.92
ExecuteBatchMethod · 0.80
HeaderMethod · 0.80
WriteHeaderMethod · 0.80
ContextMethod · 0.65
StartMethod · 0.65
ErrorMethod · 0.65
StopMethod · 0.65
generateSQLMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected