From f2eb23cd87cf32b8fe545178143b5f49e06a58da Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 8 Dec 2020 21:16:10 +0100 Subject: emit more specific status codes on video upload (#3423) - reduce http status codes list to potentially useful codes - convert more codes to typed ones - factorize html generator for error responses --- shared/core-utils/miscs/http-error-codes.ts | 111 ++++++---------------------- shared/extra-utils/server/activitypub.ts | 3 +- shared/extra-utils/server/redundancy.ts | 8 +- shared/extra-utils/users/login.ts | 13 ++-- shared/extra-utils/videos/video-history.ts | 8 +- shared/extra-utils/videos/videos.ts | 4 +- 6 files changed, 49 insertions(+), 98 deletions(-) (limited to 'shared') diff --git a/shared/core-utils/miscs/http-error-codes.ts b/shared/core-utils/miscs/http-error-codes.ts index 8c8b87ba0..9ac8a6c83 100644 --- a/shared/core-utils/miscs/http-error-codes.ts +++ b/shared/core-utils/miscs/http-error-codes.ts @@ -1,6 +1,8 @@ /** * Hypertext Transfer Protocol (HTTP) response status codes. * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes} + * + * WebDAV and other codes useless with regards to PeerTube are not listed. */ export enum HttpStatusCode { @@ -47,15 +49,6 @@ export enum HttpStatusCode { */ ACCEPTED_202 = 202, - /** - * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.4 - * - * SINCE HTTP/1.1 - * The server is a transforming proxy that received a 200 OK from its origin, - * but is returning a modified version of the origin's response. - */ - NON_AUTHORITATIVE_INFORMATION_203 = 203, - /** * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5 * @@ -77,18 +70,6 @@ export enum HttpStatusCode { */ PARTIAL_CONTENT_206 = 206, - /** - * The message body that follows is an XML message and can contain a number of separate response codes, - * depending on how many sub-requests were made. - */ - MULTI_STATUS_207 = 207, - - /** - * The server has fulfilled a request for the resource, - * and the response is a representation of the result of one or more instance-manipulations applied to the current instance. - */ - IM_USED_226 = 226, - /** * Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation). * For example, this code could be used to present multiple video format options, @@ -128,20 +109,6 @@ export enum HttpStatusCode { */ NOT_MODIFIED_304 = 304, - /** - * @deprecated - * SINCE HTTP/1.1 - * The requested resource is available only through a proxy, the address for which is provided in the response. - * Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status - * code, primarily for security reasons. - */ - USE_PROXY_305 = 305, - - /** - * No longer used. Originally meant "Subsequent requests should use the specified proxy." - */ - SWITCH_PROXY_306 = 306, - /** * SINCE HTTP/1.1 * In this case, the request should be repeated with another URI; however, future requests should still use the original URI. @@ -175,6 +142,8 @@ export enum HttpStatusCode { UNAUTHORIZED_401 = 401, /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.2 + * * Reserved for future use. The original intention was that this code might be used as part of some form of digital * cash or micro payment scheme, but that has not happened, and this code is not usually used. * Google Developers API uses this status if a particular developer has exceeded the daily limit on requests. @@ -210,11 +179,6 @@ export enum HttpStatusCode { */ NOT_ACCEPTABLE_406 = 406, - /** - * The client must first authenticate itself with the proxy. - */ - PROXY_AUTHENTICATION_REQUIRED_407 = 407, - /** * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7 * @@ -222,10 +186,14 @@ export enum HttpStatusCode { * It means that the server would like to shut down this unused connection. This response is used much more since * some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also * note that some servers merely shut down the connection without sending this message. + * + * @ */ REQUEST_TIMEOUT_408 = 408, /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.8 + * * Indicates that the request could not be processed because of conflict in the request, * such as an edit conflict between multiple simultaneous updates. */ @@ -284,52 +252,39 @@ export enum HttpStatusCode { RANGE_NOT_SATISFIABLE_416 = 416, /** - * The server cannot meet the requirements of the Expect request-header field. + * The server cannot meet the requirements of the `Expect` request-header field. */ EXPECTATION_FAILED_417 = 417, /** + * Official Documentation @ https://tools.ietf.org/html/rfc2324 + * * This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol, * and is not expected to be implemented by actual HTTP servers. The RFC specifies this code should be returned by * teapots requested to brew coffee. This HTTP status is used as an Easter egg in some websites, including PeerTube instances ;-). */ I_AM_A_TEAPOT_418 = 418, - /** - * The request was directed at a server that is not able to produce a response (for example because a connection reuse). - */ - MISDIRECTED_REQUEST_421 = 421, - /** * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3 * * The request was well-formed but was unable to be followed due to semantic errors. + * + * @see HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 if the `Content-Type` was not supported. + * @see HttpStatusCode.BAD_REQUEST_400 if the request was not parsable (broken JSON, XML) */ UNPROCESSABLE_ENTITY_422 = 422, /** - * The resource that is being accessed is locked. + * Official Documentation @ https://tools.ietf.org/html/rfc4918#section-11.3 + * + * The resource that is being accessed is locked. WebDAV-specific but used by some HTTP services. + * + * @deprecated use `If-Match` / `If-None-Match` instead + * @see {@link https://evertpot.com/http/423-locked} */ LOCKED_423 = 423, - /** - * The request failed due to failure of a previous request (e.g., a PROPPATCH). - */ - FAILED_DEPENDENCY_424 = 424, - - /** - * The client should switch to a different protocol such as TLS/1.0, given in the Upgrade header field. - */ - UPGRADE_REQUIRED_426 = 426, - - /** - * The origin server requires the request to be conditional. - * Intended to prevent "the 'lost update' problem, where a client - * GETs a resource's state, modifies it, and PUTs it back to the server, - * when meanwhile a third party has modified the state on the server, leading to a conflict." - */ - PRECONDITION_REQUIRED_428 = 428, - /** * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4 * @@ -359,6 +314,8 @@ export enum HttpStatusCode { INTERNAL_SERVER_ERROR_500 = 500, /** + * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2 + * * The server either does not recognize the request method, or it lacks the ability to fulfill the request. * Usually this implies future availability (e.g., a new feature of a web-service API). */ @@ -385,11 +342,6 @@ export enum HttpStatusCode { */ HTTP_VERSION_NOT_SUPPORTED_505 = 505, - /** - * Transparent content negotiation for the request results in a circular reference. - */ - VARIANT_ALSO_NEGOTIATES_506 = 506, - /** * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 * @@ -397,23 +349,8 @@ export enum HttpStatusCode { * server is unable to store the representation needed to successfully complete the request. This condition is * considered to be temporary. If the request which received this status code was the result of a user action, * the request MUST NOT be repeated until it is requested by a separate user action. + * + * @see HttpStatusCode.PAYLOAD_TOO_LARGE_413 for quota errors */ INSUFFICIENT_STORAGE_507 = 507, - - /** - * The server detected an infinite loop while processing the request. - */ - LOOP_DETECTED_508 = 508, - - /** - * Further extensions to the request are required for the server to fulfill it. - */ - NOT_EXTENDED_510 = 510, - - /** - * The client needs to authenticate to gain network access. - * Intended for use by intercepting proxies used to control access to the network (e.g., "captive portals" used - * to require agreement to Terms of Service before granting full Internet access via a Wi-Fi hotspot). - */ - NETWORK_AUTHENTICATION_REQUIRED_511 = 511 } diff --git a/shared/extra-utils/server/activitypub.ts b/shared/extra-utils/server/activitypub.ts index eccb198ca..cf967ed7d 100644 --- a/shared/extra-utils/server/activitypub.ts +++ b/shared/extra-utils/server/activitypub.ts @@ -1,6 +1,7 @@ import * as request from 'supertest' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) { +function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { return request(url) .get(path) .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8') diff --git a/shared/extra-utils/server/redundancy.ts b/shared/extra-utils/server/redundancy.ts index 3aca4ebfd..b83815a37 100644 --- a/shared/extra-utils/server/redundancy.ts +++ b/shared/extra-utils/server/redundancy.ts @@ -2,7 +2,13 @@ import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequ import { VideoRedundanciesTarget } from '@shared/models' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) { +function updateRedundancy ( + url: string, + accessToken: string, + host: string, + redundancyAllowed: boolean, + expectedStatus = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/server/redundancy/' + host return makePutBodyRequest({ diff --git a/shared/extra-utils/users/login.ts b/shared/extra-utils/users/login.ts index 275bb0826..39e1a2747 100644 --- a/shared/extra-utils/users/login.ts +++ b/shared/extra-utils/users/login.ts @@ -2,12 +2,13 @@ import * as request from 'supertest' import { ServerInfo } from '../server/servers' import { getClient } from '../server/clients' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' type Client = { id: string, secret: string } type User = { username: string, password: string } type Server = { url: string, client: Client, user: User } -function login (url: string, client: Client, user: User, expectedStatus = 200) { +function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/token' const body = { @@ -27,7 +28,7 @@ function login (url: string, client: Client, user: User, expectedStatus = 200) { .expect(expectedStatus) } -function logout (url: string, token: string, expectedStatus = 200) { +function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/revoke-token' return request(url) @@ -38,12 +39,12 @@ function logout (url: string, token: string, expectedStatus = 200) { } async function serverLogin (server: Server) { - const res = await login(server.url, server.client, server.user, 200) + const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200) return res.body.access_token as string } -function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = 200) { +function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/token' const body = { @@ -61,7 +62,7 @@ function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus .expect(expectedStatus) } -async function userLogin (server: Server, user: User, expectedStatus = 200) { +async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) { const res = await login(server.url, server.client, user, expectedStatus) return res.body.access_token as string @@ -95,7 +96,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) { return Promise.all(tasks) } -function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = 200) { +function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/token' const body = { diff --git a/shared/extra-utils/videos/video-history.ts b/shared/extra-utils/videos/video-history.ts index 2d751cf14..0dd3afb24 100644 --- a/shared/extra-utils/videos/video-history.ts +++ b/shared/extra-utils/videos/video-history.ts @@ -1,7 +1,13 @@ import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function userWatchVideo (url: string, token: string, videoId: number | string, currentTime: number, statusCodeExpected = 204) { +function userWatchVideo ( + url: string, + token: string, + videoId: number | string, + currentTime: number, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/' + videoId + '/watching' const fields = { currentTime } diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index a4b9d688e..a2438d712 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -155,7 +155,7 @@ function getVideosListWithToken (url: string, token: string, query: { nsfw?: boo .set('Authorization', 'Bearer ' + token) .query(immutableAssign(query, { sort: 'name' })) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -166,7 +166,7 @@ function getLocalVideos (url: string) { .get(path) .query({ sort: 'name', filter: 'local' }) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -- cgit v1.2.3