summaryrefslogtreecommitdiff
path: root/api/server/httputils
Commit message (Collapse)AuthorAgeFilesLines
* chore: use http constants to replace numbersxin.li2023-02-211-6/+6
| | | | Signed-off-by: xin.li <xin.li@daocloud.io>
* api: Extract parsing reference from repo and tagPaweł Gronowski2023-02-071-0/+35
| | | | Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
* api/server/httputils: move WriteJSON() together with ReadJSON()Sebastiaan van Stijn2022-04-112-15/+9
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* api/server/httputils: add ReadJSON() utilitySebastiaan van Stijn2022-04-112-1/+133
| | | | | | | | | | | | | | | | | | | | | Implement a ReadJSON() utility to help reduce some code-duplication, and to make sure we handle JSON requests consistently (e.g. always check for the content-type). Differences compared to current handling: - prevent possible panic if request.Body is nil ("should never happen") - always require Content-Type to be "application/json" - be stricter about additional content after JSON (previously ignored) - but, allow the body to be empty (an empty body is not invalid); update TestContainerInvalidJSON accordingly, which was testing the wrong expectation. - close body after reading (some code did this) We should consider to add a "max body size" on this function, similar to https://github.com/moby/moby/blob/7b9275c0da707b030e62c96b679a976f31f929d3/api/server/middleware/debug.go#L27-L40 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* api/server/httputils: matchesContentType(): return error instead of loggingSebastiaan van Stijn2022-04-112-18/+25
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* errdefs: move GetHTTPErrorStatusCode to api/server/httpstatusSebastiaan van Stijn2022-03-212-35/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts the changes made in 2a9c987e5a72549775ffa4dc31595ceff4f06a78, which moved the GetHTTPErrorStatusCode() utility to the errdefs package. While it seemed to make sense at the time to have the errdefs package provide conversion both from HTTP status codes errdefs and the reverse, a side-effect of the move was that the errdefs package now had a dependency on various external modules, to handle conversio of errors coming from those sub-systems, such as; - github.com/containerd/containerd - github.com/docker/distribution - google.golang.org/grpc This patch moves the conversion from (errdef-) errors to HTTP status-codes to a api/server/httpstatus package, which is only used by the API server, and should not be needed by client-code using the errdefs package. The MakeErrorHandler() utility was moved to the API server itself, as that's the only place it's used. While the same applies to the GetHTTPErrorStatusCode func, I opted for keeping that in its own package for a slightly cleaner interface. Why not move it into the api/server/httputils package? The api/server/httputils package is also imported in the client package, which uses the httputils.ParseForm() and httputils.HijackConnection() functions as part of the TestTLSCloseWriter() test. While this is only used in tests, I wanted to avoid introducing the indirect depdencencies outside of the api/server code. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* api/server: use constants for http methodsSebastiaan van Stijn2019-10-131-6/+6
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* API: explicitely ignore unhandled errorsSebastiaan van Stijn2019-09-182-6/+6
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Move httputils error helpers to errdefs packageSebastiaan van Stijn2019-03-164-295/+35
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Add httputils.FromStatusCode()Sebastiaan van Stijn2019-03-162-0/+139
| | | | | | | | | This utility allows a client to convert an API response back to a typed error; allowing the client to perform different actions based on the type of error, without having to resort to string-matching the error. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Handle correct status codes for distribution errorsSebastiaan van Stijn2019-02-081-1/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This assists to address a regression where distribution errors were not properly handled, resulting in a generic 500 (internal server error) to be returned for `/distribution/name/json` if you weren't authenticated, whereas it should return a 40x (401). This patch attempts to extract the HTTP status-code that was returned by the distribution code, and falls back to returning a 500 status if unable to match. Before this change: curl -v --unix-socket /var/run/docker.sock http://localhost/distribution/name/json * Trying /var/run/docker.sock... * Connected to localhost (/var/run/docker.sock) port 80 (#0) > GET /distribution/name/json HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.37 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Tue, 03 Jul 2018 15:52:53 GMT < Content-Length: 115 < {"message":"errors:\ndenied: requested access to the resource is denied\nunauthorized: authentication required\n"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact daemon logs: DEBU[2018-07-03T15:52:51.424950601Z] Calling GET /distribution/name/json DEBU[2018-07-03T15:52:53.179895572Z] FIXME: Got an API for which error does not match any expected type!!!: errors: denied: requested access to the resource is denied unauthorized: authentication required error_type=errcode.Errors module=api ERRO[2018-07-03T15:52:53.179942783Z] Handler for GET /distribution/name/json returned error: errors: denied: requested access to the resource is denied unauthorized: authentication required With this patch applied: curl -v --unix-socket /var/run/docker.sock http://localhost/distribution/name/json * Trying /var/run/docker.sock... * Connected to localhost (/var/run/docker.sock) port 80 (#0) > GET /distribution/name/json HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > < HTTP/1.1 403 Forbidden < Api-Version: 1.38 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Fri, 03 Aug 2018 14:58:09 GMT < Content-Length: 115 < {"message":"errors:\ndenied: requested access to the resource is denied\nunauthorized: authentication required\n"} * Curl_http_done: called premature == 0 * Connection #0 to host localhost left intact daemon logs: DEBU[2018-08-03T14:58:08.018726228Z] Calling GET /distribution/name/json Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Replace deprecated grpc.ErrorDesc() and grpc.Code() callsSebastiaan van Stijn2018-12-301-3/+3
| | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* api/server, dockerversion: modify context keyKimMachineGun2018-08-221-4/+2
| | | | Signed-off-by: KimMachineGun <geon0250@gmail.com>
* Switch from x/net/context -> contextKir Kolyshkin2018-04-232-3/+2
| | | | | | | | | | | Since Go 1.7, context is a standard package. Since Go 1.9, everything that is provided by "x/net/context" is a couple of type aliases to types in "context". Many vendored packages still use x/net/context, so vendor entry remains for now. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
* Add canonical import commentDaniel Nephin2018-02-058-8/+8
| | | | Signed-off-by: Daniel Nephin <dnephin@docker.com>
* Merge pull request #35638 from cpuguy83/error_helpers2Yong Tang2018-01-152-19/+6
|\ | | | | Add helpers to create errdef errors
| * Move api/errdefs to errdefsBrian Goff2018-01-112-2/+2
| | | | | | | | Signed-off-by: Brian Goff <cpuguy83@gmail.com>
| * Add helpers to create errdef errorsBrian Goff2018-01-112-18/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Instead of having to create a bunch of custom error types that are doing nothing but wrapping another error in sub-packages, use a common helper to create errors of the requested type. e.g. instead of re-implementing this over and over: ```go type notFoundError struct { cause error } func(e notFoundError) Error() string { return e.cause.Error() } func(e notFoundError) NotFound() {} func(e notFoundError) Cause() error { return e.cause } ``` Packages can instead just do: ``` errdefs.NotFound(err) ``` Signed-off-by: Brian Goff <cpuguy83@gmail.com>
* | Golint: don't use basic untyped string for context keySebastiaan van Stijn2018-01-151-1/+3
|/ | | | Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Move RFC3339NanoFixed to a more appropriate package.Daniel Nephin2017-09-251-6/+2
| | | | Signed-off-by: Daniel Nephin <dnephin@docker.com>
* LCOW: Implemented support for docker cp + buildAkash Gupta2017-09-141-3/+1
| | | | | | | | | | This enables docker cp and ADD/COPY docker build support for LCOW. Originally, the graphdriver.Get() interface returned a local path to the container root filesystem. This does not work for LCOW, so the Get() method now returns an interface that LCOW implements to support copying to and from the container. Signed-off-by: Akash Gupta <akagup@microsoft.com>
* Add gosimple linterDaniel Nephin2017-09-121-9/+2
| | | | | | Update gometalinter Signed-off-by: Daniel Nephin <dnephin@docker.com>
* Add deadcode linterDaniel Nephin2017-08-211-8/+0
| | | | Signed-off-by: Daniel Nephin <dnephin@docker.com>
* Remove string checking in API error handlingBrian Goff2017-08-153-53/+69
| | | | | | | | | | | | | | Use strongly typed errors to set HTTP status codes. Error interfaces are defined in the api/errors package and errors returned from controllers are checked against these interfaces. Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the line of causes one of the interfaces is implemented. The special error interfaces take precedence over Causer, meaning if both Causer and one of the new error interfaces are implemented, the Causer is not traversed. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
* Update logrus to v1.0.1Derek McGowan2017-07-312-2/+2
| | | | | | Fixes case sensitivity issue Signed-off-by: Derek McGowan <derek@mcgstyle.net>
* Avoid using a map for log attributesAaron Lehmann2017-07-181-19/+21
| | | | | | | | | | | | | Having a map per log entry seemed heavier than necessary. These attributes end up being sorted and serialized, so storing them in a map doesn't add anything (there's no random access element). In SwarmKit, they originate as a slice, so there's an unnecessary conversion to a map and back. This also fixes the sort comparator, which used to inefficiently split the string on each comparison. Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
* Merge pull request #33798 from vdemeester/move-some-api-code-awayVincent Demeester2017-06-242-3/+30
|\ | | | | Move some `api` package functions away
| * Move some `api` package functions awayVincent Demeester2017-06-232-3/+30
| | | | | | | | | | | | | | | | - DisplayablePorts is a `cli` function, moving to `docker/cli` - Move MatchesContentType to the only package using it, `api/server/httputils` (and remove the deps on logrus for `api` package) Signed-off-by: Vincent Demeester <vincent@sbr.pm>
* | Remove Go 1.6 code from mobyBoaz Shuster2017-06-192-18/+0
|/ | | | Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
* Suggest login on pull denialAlfred Landrum2017-06-091-0/+1
| | | | Signed-off-by: Alfred Landrum <alfred.landrum@docker.com>
* choose rpc code to determine status codeallencloud2017-06-061-0/+40
| | | | Signed-off-by: allencloud <allen.sun@daocloud.io>
* Change log details encoding format and support details on service logsDrew Erny2017-05-121-0/+2
| | | | | | | | | | URL query encode log details, so that characters like spaces don't make log parsing ambiguous. Add a helper function to parse these details to a map, if needed Add support for details on service logs Signed-off-by: Drew Erny <drew.erny@docker.com>
* refactor logs and support service logs /w ttyDrew Erny2017-04-061-0/+92
| | | | | | | | | Refactor container logs system to make communicating log messages internally much simpler. Move responsibility for marshalling log messages into the REST server. Support TTY logs. Pave the way for fixing the ambiguous bytestream format. Pave the way for fixing details. Signed-off-by: Drew Erny <drew.erny@docker.com>
* use t.Fatal() to output the err message where the values used for formattingfate-grand-order2017-02-231-1/+1
| | | | | | text does not appear to contain a placeholder Signed-off-by: Helen Xie <chenjg@harmonycloud.cn>
* fix status-code for "locked" swarm and "certificate expired"Sebastiaan van Stijn2017-02-171-0/+2
| | | | | | | These errors were producing the wrong status code, changing to 503. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
* Fix #303111: dockerd leaks ExecIds on failed exec -iDmitry Shyshkin2017-02-101-0/+1
| | | | Signed-off-by: Dmitry Shyshkin <dmitry@shyshkin.org.ua>
* fix some types on errors and formkaiwentan2017-02-072-2/+2
| | | | Signed-off-by: kaiwentan <kaiwentan@harmonycloud.cn>
* Merge pull request #28631 from likel/masterSebastiaan van Stijn2016-12-301-3/+3
|\ | | | | Don't do format if it's unnecessary
| * Remove redundant formatKe Li2016-12-271-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | Signed-off-by: Ke Li <kel@splunk.com> Add missing changes Signed-off-by: Ke Li <kel@splunk.com> User errors.New to create error Signed-off-by: Ke Li <kel@splunk.com>
* | Move UAStringKey to dockerversion pkgTonis Tiigi2016-12-261-3/+0
|/ | | | | Removes grpc dependency from client Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
* return directly without ifs in remaining packagesunclejack2016-12-141-4/+1
| | | | Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
* fix typoallencloud2016-11-181-1/+1
| | | | Signed-off-by: allencloud <allen.sun@daocloud.io>
* Merge pull request #28532 from stevvooe/disabled-swarm-service-unavailableMichael Crosby2016-11-171-1/+1
|\ | | | | api/server/httputils: service unavailable for disable swarm
| * api/server/httputils: service unavailable for disable swarmStephen J Day2016-11-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When swarm-mode is disabled, we need to return an error indicating this. 406 was chosen for the "Not Acceptable" verbiage, but this code has specific semantics in relation to the `Accept` header, which aren't applicable here. We now use a 503 for this case. While it is not a perfect match, it does make it clear that the particular "service" (read: API endpoint) is not available. The body of the message provides the user with enough information to take action on it by enabling swarm-mode and ensuring the service is available. Signed-off-by: Stephen J Day <stephen.day@docker.com>
* | Merge pull request #28383 from zteBill/fix-useless-varBrian Goff2016-11-171-6/+7
|\ \ | |/ |/| Remove needless var
| * make each return explicitlyzteBill2016-11-171-6/+7
| | | | | | | | Signed-off-by: zteBill <bi.zhenkun@zte.com.cn>
* | api/server/httputils: ensure consistent status codeStephen J Day2016-11-161-13/+16
|/ | | | | | | | | | | Error code resolution is powered by string matching. Not the greatest thing in the world and I hope no one is proud of this code, but it works. However, because a map is used, the iteration order of the map is random, such that if an error matches two of the snippets, it may return a different error code depending on the seed of the hashmap. This change converts it to use a slice instead. Signed-off-by: Stephen J Day <stephen.day@docker.com>
* api/server/httputils: compile with go < 1.7Antonio Murdaca2016-10-043-10/+33
| | | | Signed-off-by: Antonio Murdaca <runcom@redhat.com>
* Disable HTML escaping for JSON strings in `docker inspect`Yong Tang2016-09-291-1/+3
| | | | | | | | | | | | | | | | | This fix tries to address the issue raised in 27021 where HTML strings like (`&, >, <, etc`) in environmental variables are escaped for JSON output for `docker inspect`. For example, `TEST_ENV="soanni&rtr"` has been escaped to `TEST_ENV="soanni\u0026rtr"` This fix disabled HTML escaping with `SetEscapeHTML`, which is available since golang 1.7.0. This changes will be applied to all JSON output that utilize `httputils.WriteJSON`. An integration test has been added to cover the changes. This fix fixes 27021. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
* Use apiError in server version middleware.Daniel Nephin2016-09-161-1/+6
| | | | Signed-off-by: Daniel Nephin <dnephin@docker.com>