GitLabgitlab-org
cli
main
1package api
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "mime/multipart"
10 "net/http"
11 "net/url"
12 "os"
13 "path/filepath"
14 "strconv"
15 "strings"
17 "gitlab.com/gitlab-org/cli/internal/api"
18)
20// httpRequest labels an io.Reader body as JSON only when rawBodyIsJSON is set.
21func httpRequest(ctx context.Context, client *api.Client, method, p string, params any, headers []string, rawBodyIsJSON bool) (*http.Response, error) {
22 var err error
23 isGraphQL := p == "graphql"
25 baseURL := client.Lab().BaseURL()
26 baseURLStr := baseURL.String()
27 switch {
28 case strings.Contains(p, "://"):
29 baseURLStr = p
30 case isGraphQL:
31 baseURL.Path = strings.TrimSuffix(strings.TrimSuffix(baseURL.Path, "/"), "/api/v4") + "/api/graphql"
32 baseURLStr = baseURL.String()
33 default:
34 baseURLStr += strings.TrimPrefix(p, "/")
35 }
37 var body io.Reader
38 var bodyIsJSON bool
39 switch pp := params.(type) {
40 case map[string]any:
41 if isQueryMethod(method) {
42 baseURLStr, err = parseQuery(baseURLStr, pp)
43 if err != nil {
44 return nil, err
45 }
46 } else {
47 for key, value := range pp {
48 if vv, ok := value.([]byte); ok {
49 pp[key] = string(vv)
50 }
51 }
52 if isGraphQL {
53 pp = groupGraphQLVariables(pp)
54 }
56 b, err := json.Marshal(pp) //nolint:forbidigo // building HTTP request body, not stdout
57 if err != nil {
58 return nil, fmt.Errorf("error serializing parameters: %w", err)
59 }
60 body = bytes.NewBuffer(b)
61 bodyIsJSON = true
62 }
63 case io.Reader:
64 body = pp
65 bodyIsJSON = rawBodyIsJSON
66 case nil:
67 body = nil
68 default:
69 return nil, fmt.Errorf("unrecognized parameter type: %v", params)
70 }
72 reqURL, err := url.Parse(baseURLStr)
73 if err != nil {
74 return nil, fmt.Errorf("invalid request URL: %w", err)
75 }
76 req, err := api.NewHTTPRequest(ctx, client, method, reqURL, body, headers, bodyIsJSON)
77 if err != nil {
78 return nil, err
79 }
80 return client.HTTPClient().Do(req)
81}
83func groupGraphQLVariables(params map[string]any) map[string]any {
84 topLevel := make(map[string]any)
85 variables := make(map[string]any)
87 for key, val := range params {
88 switch key {
89 case "query", "operationName":
90 topLevel[key] = val
91 default:
92 variables[key] = val
93 }
94 }
96 if len(variables) > 0 {
97 topLevel["variables"] = variables
98 }
99 return topLevel
100}
102// isQueryMethod reports whether fields for this method are sent as a query
103// string rather than a request body.
104func isQueryMethod(method string) bool {
105 return strings.EqualFold(method, http.MethodGet) || strings.EqualFold(method, http.MethodDelete)
106}
108// queryList holds the values repeated flags gave one name ending in "[]". Unlike
109// a decoded JSON array, the name already carries its "[]", so none is appended.
110type queryList []any
112func parseQuery(path string, params map[string]any) (string, error) {
113 if len(params) == 0 {
114 return path, nil
115 }
116 q := url.Values{}
117 for key, value := range params {
118 switch v := value.(type) {
119 case []any:
120 // Encode JSON arrays as repeated key[]= parameters, the form the
121 // GitLab REST API expects for array query parameters. A name given
122 // with its own "[]" already spells that form, so appending a second
123 // pair would address key[][] instead.
124 arrayKey := key
125 if !strings.HasSuffix(key, "[]") {
126 arrayKey = key + "[]"
127 }
128 for _, item := range v {
129 s, err := queryScalarValue(item)
130 if err != nil {
131 return "", fmt.Errorf("query parameter %q: %w", key, err)
132 }
133 q.Add(arrayKey, s)
134 }
135 case queryList:
136 for _, item := range v {
137 s, err := queryLiteralValue(item)
138 if err != nil {
139 return "", fmt.Errorf("query parameter %q: %w", key, err)
140 }
141 q.Add(key, s)
142 }
143 case map[string]any:
144 return "", fmt.Errorf("query parameter %q: objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body", key)
145 default:
146 s, err := queryLiteralValue(v)
147 if err != nil {
148 return "", fmt.Errorf("query parameter %q: %w", key, err)
149 }
150 q.Add(key, s)
151 }
152 }
154 // An empty array contributes no parameters, so q can still be empty here
155 // even though params was not. Appending a separator in that case would
156 // produce a trailing "?" and, alongside other parameters, silently drop the
157 // empty one.
158 if len(q) == 0 {
159 return path, nil
160 }
162 sep := "?"
163 if strings.ContainsRune(path, '?') {
164 sep = "&"
165 }
166 return path + sep + q.Encode(), nil
167}
169// queryLiteralValue renders a value that occupies a query parameter by itself.
170func queryLiteralValue(v any) (string, error) {
171 switch t := v.(type) {
172 case string:
173 return t, nil
174 case []byte:
175 return string(t), nil
176 case nil:
177 return "", nil
178 case int:
179 return fmt.Sprintf("%d", t), nil
180 case bool:
181 return fmt.Sprintf("%v", t), nil
182 case []any, map[string]any:
183 // queryScalarValue owns the wording, so both paths report it identically.
184 return queryScalarValue(t)
185 default:
186 return "", fmt.Errorf("unknown type %v", t)
187 }
188}
190// queryScalarValue renders a single JSON-decoded array element as a query
191// string value. Numbers decode as json.Number (see magicFieldValue), so they
192// render exactly as written.
193func queryScalarValue(v any) (string, error) {
194 switch t := v.(type) {
195 case string:
196 return t, nil
197 case bool:
198 return strconv.FormatBool(t), nil
199 case json.Number:
200 return t.String(), nil
201 case nil:
202 // A query string cannot express null. Encoding it as an empty value
203 // would silently turn null into "", so reject it instead.
204 return "", fmt.Errorf("null is not supported as an array element in a query parameter; omit the element or use --input with a POST, PUT, or PATCH request body")
205 default:
206 return "", fmt.Errorf("nested arrays and objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body")
207 }
208}
210// buildMultipartBody constructs a multipart/form-data request body from a slice
211// of raw "key=value" or "key=@filepath" strings. File fields use the @filepath
212// syntax; all other fields are written as plain text parts. The returned
213// contentType string includes the boundary and must be set as the request's
214// Content-Type header.
215//
216// The body is streamed via io.Pipe so that file content is never fully buffered
217// in memory.
218func buildMultipartBody(formFields []string, stdin io.ReadCloser) (io.Reader, string) {
219 pr, pw := io.Pipe()
220 w := multipart.NewWriter(pw)
221 contentType := w.FormDataContentType()
223 go func() {
224 err := writeMultipartFields(w, formFields, stdin)
225 if closeErr := w.Close(); err == nil {
226 err = closeErr
227 }
228 pw.CloseWithError(err)
229 }()
231 return pr, contentType
232}
234// writeMultipartFields writes all form fields to w. It is called from a
235// goroutine inside buildMultipartBody.
236func writeMultipartFields(w *multipart.Writer, formFields []string, stdin io.ReadCloser) error {
237 for _, f := range formFields {
238 key, value, err := parseField(f)
239 if err != nil {
240 return err
241 }
242 if path, isFile := strings.CutPrefix(value, "@"); isFile {
243 if err := copyFileField(w, key, path, stdin); err != nil {
244 return err
245 }
246 } else {
247 if err := w.WriteField(key, value); err != nil {
248 return err
249 }
250 }
251 }
252 return nil
253}
255// copyFileField writes a single file part to w. Each call opens and closes its
256// own file handle, so file descriptors are not accumulated across loop iterations.
257func copyFileField(w *multipart.Writer, key, path string, stdin io.ReadCloser) error {
258 var r io.Reader
259 var filename string
260 if path == "-" {
261 r = stdin
262 filename = "-"
263 } else {
264 fh, err := os.Open(path)
265 if err != nil {
266 return err
267 }
268 defer fh.Close()
269 r = fh
270 filename = filepath.Base(path)
271 }
272 fw, err := w.CreateFormFile(key, filename)
273 if err != nil {
274 return err
275 }
276 _, err = io.Copy(fw, r)
277 return err
278}
Diagnostics7 git calls · 224 ms
CallStatusSizeTime0224 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:02001.8 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200285 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=285 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=48bbcb7832066dead74ecdecb6257183f3d03bf0 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=285 git.pkt.payload.max=258 git.sideband.band1.bytes=258 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=258 git.pack.trailer=2570819fa1caef7b0fd3f7ba2d1b1a658b78702eOpen pack map
fetch2002.8 KiB214 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=214 git.response.bytes=2890 git.response.cache=MISS git.response.duration_ms=214 git.fetch.wants=a1fe2eec0dc0a936a2b8ad7d9b9590bf42b63ec6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2890 git.pkt.payload.max=2863 git.sideband.band1.bytes=2863 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1f2d9de20efe9
- Request
- 018346e9…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format