aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-08 21:16:10 +0100
committerGitHub <noreply@github.com>2020-12-08 21:16:10 +0100
commitf2eb23cd87cf32b8fe545178143b5f49e06a58da (patch)
treeaf7d59945af70e28fd85047e2c688c59a908f548 /shared
parentc977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff)
downloadPeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.gz
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.zst
PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.zip
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
Diffstat (limited to 'shared')
-rw-r--r--shared/core-utils/miscs/http-error-codes.ts111
-rw-r--r--shared/extra-utils/server/activitypub.ts3
-rw-r--r--shared/extra-utils/server/redundancy.ts8
-rw-r--r--shared/extra-utils/users/login.ts13
-rw-r--r--shared/extra-utils/videos/video-history.ts8
-rw-r--r--shared/extra-utils/videos/videos.ts4
6 files changed, 49 insertions, 98 deletions
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 @@
1/** 1/**
2 * Hypertext Transfer Protocol (HTTP) response status codes. 2 * Hypertext Transfer Protocol (HTTP) response status codes.
3 * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes} 3 * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes}
4 *
5 * WebDAV and other codes useless with regards to PeerTube are not listed.
4 */ 6 */
5export enum HttpStatusCode { 7export enum HttpStatusCode {
6 8
@@ -48,15 +50,6 @@ export enum HttpStatusCode {
48 ACCEPTED_202 = 202, 50 ACCEPTED_202 = 202,
49 51
50 /** 52 /**
51 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.4
52 *
53 * SINCE HTTP/1.1
54 * The server is a transforming proxy that received a 200 OK from its origin,
55 * but is returning a modified version of the origin's response.
56 */
57 NON_AUTHORITATIVE_INFORMATION_203 = 203,
58
59 /**
60 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5 53 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5
61 * 54 *
62 * There is no content to send for this request, but the headers may be useful. 55 * There is no content to send for this request, but the headers may be useful.
@@ -78,18 +71,6 @@ export enum HttpStatusCode {
78 PARTIAL_CONTENT_206 = 206, 71 PARTIAL_CONTENT_206 = 206,
79 72
80 /** 73 /**
81 * The message body that follows is an XML message and can contain a number of separate response codes,
82 * depending on how many sub-requests were made.
83 */
84 MULTI_STATUS_207 = 207,
85
86 /**
87 * The server has fulfilled a request for the resource,
88 * and the response is a representation of the result of one or more instance-manipulations applied to the current instance.
89 */
90 IM_USED_226 = 226,
91
92 /**
93 * Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation). 74 * Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation).
94 * For example, this code could be used to present multiple video format options, 75 * For example, this code could be used to present multiple video format options,
95 * to list files with different filename extensions, or to suggest word-sense disambiguation. 76 * to list files with different filename extensions, or to suggest word-sense disambiguation.
@@ -129,20 +110,6 @@ export enum HttpStatusCode {
129 NOT_MODIFIED_304 = 304, 110 NOT_MODIFIED_304 = 304,
130 111
131 /** 112 /**
132 * @deprecated
133 * SINCE HTTP/1.1
134 * The requested resource is available only through a proxy, the address for which is provided in the response.
135 * Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status
136 * code, primarily for security reasons.
137 */
138 USE_PROXY_305 = 305,
139
140 /**
141 * No longer used. Originally meant "Subsequent requests should use the specified proxy."
142 */
143 SWITCH_PROXY_306 = 306,
144
145 /**
146 * SINCE HTTP/1.1 113 * SINCE HTTP/1.1
147 * In this case, the request should be repeated with another URI; however, future requests should still use the original URI. 114 * In this case, the request should be repeated with another URI; however, future requests should still use the original URI.
148 * In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the 115 * In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the
@@ -175,6 +142,8 @@ export enum HttpStatusCode {
175 UNAUTHORIZED_401 = 401, 142 UNAUTHORIZED_401 = 401,
176 143
177 /** 144 /**
145 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.2
146 *
178 * Reserved for future use. The original intention was that this code might be used as part of some form of digital 147 * Reserved for future use. The original intention was that this code might be used as part of some form of digital
179 * cash or micro payment scheme, but that has not happened, and this code is not usually used. 148 * cash or micro payment scheme, but that has not happened, and this code is not usually used.
180 * Google Developers API uses this status if a particular developer has exceeded the daily limit on requests. 149 * Google Developers API uses this status if a particular developer has exceeded the daily limit on requests.
@@ -211,21 +180,20 @@ export enum HttpStatusCode {
211 NOT_ACCEPTABLE_406 = 406, 180 NOT_ACCEPTABLE_406 = 406,
212 181
213 /** 182 /**
214 * The client must first authenticate itself with the proxy.
215 */
216 PROXY_AUTHENTICATION_REQUIRED_407 = 407,
217
218 /**
219 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7 183 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7
220 * 184 *
221 * This response is sent on an idle connection by some servers, even without any previous request by the client. 185 * This response is sent on an idle connection by some servers, even without any previous request by the client.
222 * It means that the server would like to shut down this unused connection. This response is used much more since 186 * It means that the server would like to shut down this unused connection. This response is used much more since
223 * some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also 187 * some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also
224 * note that some servers merely shut down the connection without sending this message. 188 * note that some servers merely shut down the connection without sending this message.
189 *
190 * @
225 */ 191 */
226 REQUEST_TIMEOUT_408 = 408, 192 REQUEST_TIMEOUT_408 = 408,
227 193
228 /** 194 /**
195 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.8
196 *
229 * Indicates that the request could not be processed because of conflict in the request, 197 * Indicates that the request could not be processed because of conflict in the request,
230 * such as an edit conflict between multiple simultaneous updates. 198 * such as an edit conflict between multiple simultaneous updates.
231 */ 199 */
@@ -284,11 +252,13 @@ export enum HttpStatusCode {
284 RANGE_NOT_SATISFIABLE_416 = 416, 252 RANGE_NOT_SATISFIABLE_416 = 416,
285 253
286 /** 254 /**
287 * The server cannot meet the requirements of the Expect request-header field. 255 * The server cannot meet the requirements of the `Expect` request-header field.
288 */ 256 */
289 EXPECTATION_FAILED_417 = 417, 257 EXPECTATION_FAILED_417 = 417,
290 258
291 /** 259 /**
260 * Official Documentation @ https://tools.ietf.org/html/rfc2324
261 *
292 * This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol, 262 * This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol,
293 * and is not expected to be implemented by actual HTTP servers. The RFC specifies this code should be returned by 263 * and is not expected to be implemented by actual HTTP servers. The RFC specifies this code should be returned by
294 * teapots requested to brew coffee. This HTTP status is used as an Easter egg in some websites, including PeerTube instances ;-). 264 * teapots requested to brew coffee. This HTTP status is used as an Easter egg in some websites, including PeerTube instances ;-).
@@ -296,41 +266,26 @@ export enum HttpStatusCode {
296 I_AM_A_TEAPOT_418 = 418, 266 I_AM_A_TEAPOT_418 = 418,
297 267
298 /** 268 /**
299 * The request was directed at a server that is not able to produce a response (for example because a connection reuse).
300 */
301 MISDIRECTED_REQUEST_421 = 421,
302
303 /**
304 * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3 269 * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3
305 * 270 *
306 * The request was well-formed but was unable to be followed due to semantic errors. 271 * The request was well-formed but was unable to be followed due to semantic errors.
272 *
273 * @see HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 if the `Content-Type` was not supported.
274 * @see HttpStatusCode.BAD_REQUEST_400 if the request was not parsable (broken JSON, XML)
307 */ 275 */
308 UNPROCESSABLE_ENTITY_422 = 422, 276 UNPROCESSABLE_ENTITY_422 = 422,
309 277
310 /** 278 /**
311 * The resource that is being accessed is locked. 279 * Official Documentation @ https://tools.ietf.org/html/rfc4918#section-11.3
280 *
281 * The resource that is being accessed is locked. WebDAV-specific but used by some HTTP services.
282 *
283 * @deprecated use `If-Match` / `If-None-Match` instead
284 * @see {@link https://evertpot.com/http/423-locked}
312 */ 285 */
313 LOCKED_423 = 423, 286 LOCKED_423 = 423,
314 287
315 /** 288 /**
316 * The request failed due to failure of a previous request (e.g., a PROPPATCH).
317 */
318 FAILED_DEPENDENCY_424 = 424,
319
320 /**
321 * The client should switch to a different protocol such as TLS/1.0, given in the Upgrade header field.
322 */
323 UPGRADE_REQUIRED_426 = 426,
324
325 /**
326 * The origin server requires the request to be conditional.
327 * Intended to prevent "the 'lost update' problem, where a client
328 * GETs a resource's state, modifies it, and PUTs it back to the server,
329 * when meanwhile a third party has modified the state on the server, leading to a conflict."
330 */
331 PRECONDITION_REQUIRED_428 = 428,
332
333 /**
334 * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4 289 * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4
335 * 290 *
336 * The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes. 291 * The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes.
@@ -359,6 +314,8 @@ export enum HttpStatusCode {
359 INTERNAL_SERVER_ERROR_500 = 500, 314 INTERNAL_SERVER_ERROR_500 = 500,
360 315
361 /** 316 /**
317 * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2
318 *
362 * The server either does not recognize the request method, or it lacks the ability to fulfill the request. 319 * The server either does not recognize the request method, or it lacks the ability to fulfill the request.
363 * Usually this implies future availability (e.g., a new feature of a web-service API). 320 * Usually this implies future availability (e.g., a new feature of a web-service API).
364 */ 321 */
@@ -386,34 +343,14 @@ export enum HttpStatusCode {
386 HTTP_VERSION_NOT_SUPPORTED_505 = 505, 343 HTTP_VERSION_NOT_SUPPORTED_505 = 505,
387 344
388 /** 345 /**
389 * Transparent content negotiation for the request results in a circular reference.
390 */
391 VARIANT_ALSO_NEGOTIATES_506 = 506,
392
393 /**
394 * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 346 * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6
395 * 347 *
396 * The 507 (Insufficient Storage) status code means the method could not be performed on the resource because the 348 * The 507 (Insufficient Storage) status code means the method could not be performed on the resource because the
397 * server is unable to store the representation needed to successfully complete the request. This condition is 349 * server is unable to store the representation needed to successfully complete the request. This condition is
398 * considered to be temporary. If the request which received this status code was the result of a user action, 350 * considered to be temporary. If the request which received this status code was the result of a user action,
399 * the request MUST NOT be repeated until it is requested by a separate user action. 351 * the request MUST NOT be repeated until it is requested by a separate user action.
352 *
353 * @see HttpStatusCode.PAYLOAD_TOO_LARGE_413 for quota errors
400 */ 354 */
401 INSUFFICIENT_STORAGE_507 = 507, 355 INSUFFICIENT_STORAGE_507 = 507,
402
403 /**
404 * The server detected an infinite loop while processing the request.
405 */
406 LOOP_DETECTED_508 = 508,
407
408 /**
409 * Further extensions to the request are required for the server to fulfill it.
410 */
411 NOT_EXTENDED_510 = 510,
412
413 /**
414 * The client needs to authenticate to gain network access.
415 * Intended for use by intercepting proxies used to control access to the network (e.g., "captive portals" used
416 * to require agreement to Terms of Service before granting full Internet access via a Wi-Fi hotspot).
417 */
418 NETWORK_AUTHENTICATION_REQUIRED_511 = 511
419} 356}
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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2 3
3function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) { 4function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
4 return request(url) 5 return request(url)
5 .get(path) 6 .get(path)
6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8') 7 .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
2import { VideoRedundanciesTarget } from '@shared/models' 2import { VideoRedundanciesTarget } from '@shared/models'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 4
5function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) { 5function updateRedundancy (
6 url: string,
7 accessToken: string,
8 host: string,
9 redundancyAllowed: boolean,
10 expectedStatus = HttpStatusCode.NO_CONTENT_204
11) {
6 const path = '/api/v1/server/redundancy/' + host 12 const path = '/api/v1/server/redundancy/' + host
7 13
8 return makePutBodyRequest({ 14 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'
2 2
3import { ServerInfo } from '../server/servers' 3import { ServerInfo } from '../server/servers'
4import { getClient } from '../server/clients' 4import { getClient } from '../server/clients'
5import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5 6
6type Client = { id: string, secret: string } 7type Client = { id: string, secret: string }
7type User = { username: string, password: string } 8type User = { username: string, password: string }
8type Server = { url: string, client: Client, user: User } 9type Server = { url: string, client: Client, user: User }
9 10
10function login (url: string, client: Client, user: User, expectedStatus = 200) { 11function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) {
11 const path = '/api/v1/users/token' 12 const path = '/api/v1/users/token'
12 13
13 const body = { 14 const body = {
@@ -27,7 +28,7 @@ function login (url: string, client: Client, user: User, expectedStatus = 200) {
27 .expect(expectedStatus) 28 .expect(expectedStatus)
28} 29}
29 30
30function logout (url: string, token: string, expectedStatus = 200) { 31function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) {
31 const path = '/api/v1/users/revoke-token' 32 const path = '/api/v1/users/revoke-token'
32 33
33 return request(url) 34 return request(url)
@@ -38,12 +39,12 @@ function logout (url: string, token: string, expectedStatus = 200) {
38} 39}
39 40
40async function serverLogin (server: Server) { 41async function serverLogin (server: Server) {
41 const res = await login(server.url, server.client, server.user, 200) 42 const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200)
42 43
43 return res.body.access_token as string 44 return res.body.access_token as string
44} 45}
45 46
46function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = 200) { 47function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) {
47 const path = '/api/v1/users/token' 48 const path = '/api/v1/users/token'
48 49
49 const body = { 50 const body = {
@@ -61,7 +62,7 @@ function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus
61 .expect(expectedStatus) 62 .expect(expectedStatus)
62} 63}
63 64
64async function userLogin (server: Server, user: User, expectedStatus = 200) { 65async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) {
65 const res = await login(server.url, server.client, user, expectedStatus) 66 const res = await login(server.url, server.client, user, expectedStatus)
66 67
67 return res.body.access_token as string 68 return res.body.access_token as string
@@ -95,7 +96,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) {
95 return Promise.all(tasks) 96 return Promise.all(tasks)
96} 97}
97 98
98function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = 200) { 99function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) {
99 const path = '/api/v1/users/token' 100 const path = '/api/v1/users/token'
100 101
101 const body = { 102 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 @@
1import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' 1import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
2import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 2import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
3 3
4function userWatchVideo (url: string, token: string, videoId: number | string, currentTime: number, statusCodeExpected = 204) { 4function userWatchVideo (
5 url: string,
6 token: string,
7 videoId: number | string,
8 currentTime: number,
9 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
10) {
5 const path = '/api/v1/videos/' + videoId + '/watching' 11 const path = '/api/v1/videos/' + videoId + '/watching'
6 const fields = { currentTime } 12 const fields = { currentTime }
7 13
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
155 .set('Authorization', 'Bearer ' + token) 155 .set('Authorization', 'Bearer ' + token)
156 .query(immutableAssign(query, { sort: 'name' })) 156 .query(immutableAssign(query, { sort: 'name' }))
157 .set('Accept', 'application/json') 157 .set('Accept', 'application/json')
158 .expect(200) 158 .expect(HttpStatusCode.OK_200)
159 .expect('Content-Type', /json/) 159 .expect('Content-Type', /json/)
160} 160}
161 161
@@ -166,7 +166,7 @@ function getLocalVideos (url: string) {
166 .get(path) 166 .get(path)
167 .query({ sort: 'name', filter: 'local' }) 167 .query({ sort: 'name', filter: 'local' })
168 .set('Accept', 'application/json') 168 .set('Accept', 'application/json')
169 .expect(200) 169 .expect(HttpStatusCode.OK_200)
170 .expect('Content-Type', /json/) 170 .expect('Content-Type', /json/)
171} 171}
172 172