2019-02-22 08:34:48 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
util "agola.io/agola/internal/util"
|
2019-02-22 08:34:48 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-22 08:34:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReposHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-04-05 14:23:54 +00:00
|
|
|
gitServerURL string
|
2019-02-22 08:34:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewReposHandler(log zerolog.Logger, gitServerURL string) *ReposHandler {
|
|
|
|
return &ReposHandler{log: log, gitServerURL: gitServerURL}
|
2019-02-22 08:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
path := vars["rest"]
|
|
|
|
|
2019-04-05 14:23:54 +00:00
|
|
|
u, err := url.Parse(h.gitServerURL)
|
2019-02-22 08:34:48 +00:00
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-22 08:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
u.Path = path
|
|
|
|
u.RawQuery = r.URL.RawQuery
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
// proxy all the request body to the destination server
|
|
|
|
req, err := http.NewRequest(r.Method, u.String(), r.Body)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-22 08:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy request headers
|
|
|
|
for k, vv := range r.Header {
|
|
|
|
for _, v := range vv {
|
|
|
|
req.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-22 08:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy response headers
|
|
|
|
for k, vv := range resp.Header {
|
|
|
|
for _, v := range vv {
|
|
|
|
w.Header().Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// copy status
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// copy response body
|
|
|
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-22 08:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|