| @@ -119,8 +119,10 @@ typedef struct HTTPContext { · 118 unmodified lines |
| 119 | 119 | char *method; |
| 120 | 120 | int reconnect; |
| 121 | 121 | int reconnect_at_eof; |
| 122 | + int reconnect_on_network_error; |
| 122 | 123 | int reconnect_streamed; |
| 123 | 124 | int reconnect_delay_max; |
| 125 | + char *reconnect_on_http_error; |
| 124 | 126 | int listen; |
| 125 | 127 | char *resource; |
| 126 | 128 | int reply_code; |
| @@ -164,6 +166,8 @@ static const AVOption options[] = { · 37 unmodified lines |
| 164 | 166 | { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E }, |
| 165 | 167 | { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, |
| 166 | 168 | { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, |
| 169 | + { "reconnect_on_network_error", "auto reconnect in case of tcp/tls error during connect", OFFSET(reconnect_on_network_error), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, |
| 170 | + { "reconnect_on_http_error", "list of http status codes to reconnect on", OFFSET(reconnect_on_http_error), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D }, |
| 167 | 171 | { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, |
| 168 | 172 | { "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D }, |
| 169 | 173 | { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E }, |
| @@ -258,21 +262,73 @@ static int http_open_cnx_internal(URLContext *h, AVDictionary **options) · 88 unmodified lines |
| 258 | 262 | return location_changed; |
| 259 | 263 | } |
| 260 | 264 | |
| 265 | +static int http_should_reconnect(HTTPContext *s, int err) |
| 266 | +{ |
| 267 | + const char *status_group; |
| 268 | + char http_code[4]; |
| 269 | + |
| 270 | + switch (err) { |
| 271 | + case AVERROR_HTTP_BAD_REQUEST: |
| 272 | + case AVERROR_HTTP_UNAUTHORIZED: |
| 273 | + case AVERROR_HTTP_FORBIDDEN: |
| 274 | + case AVERROR_HTTP_NOT_FOUND: |
| 275 | + case AVERROR_HTTP_OTHER_4XX: |
| 276 | + status_group = "4xx"; |
| 277 | + break; |
| 278 | + |
| 279 | + case AVERROR_HTTP_SERVER_ERROR: |
| 280 | + status_group = "5xx"; |
| 281 | + break; |
| 282 | + |
| 283 | + default: |
| 284 | + return s->reconnect_on_network_error; |
| 285 | + } |
| 286 | + |
| 287 | + if (!s->reconnect_on_http_error) |
| 288 | + return 0; |
| 289 | + |
| 290 | + if (av_match_list(status_group, s->reconnect_on_http_error, ',') > 0) |
| 291 | + return 1; |
| 292 | + |
| 293 | + snprintf(http_code, sizeof(http_code), "%d", s->http_code); |
| 294 | + |
| 295 | + return av_match_list(http_code, s->reconnect_on_http_error, ',') > 0; |
| 296 | +} |
| 297 | + |
| 261 | 298 | /* return non zero if error */ |
| 262 | 299 | static int http_open_cnx(URLContext *h, AVDictionary **options) |
| 263 | 300 | { |
| 264 | 301 | HTTPAuthType cur_auth_type, cur_proxy_auth_type; |
| 265 | 302 | HTTPContext *s = h->priv_data; |
| 266 | 303 | int location_changed, attempts = 0, redirects = 0; |
| 304 | + int reconnect_delay = 0; |
| 305 | + uint64_t off; |
| 306 | + |
| 267 | 307 | redo: |
| 268 | 308 | av_dict_copy(options, s->chained_options, 0); |
| 269 | 309 | |
| 270 | 310 | cur_auth_type = s->auth_state.auth_type; |
| 271 | 311 | cur_proxy_auth_type = s->auth_state.auth_type; |
| 272 | 312 | |
| 313 | + off = s->off; |
| 273 | 314 | location_changed = http_open_cnx_internal(h, options); |
| 274 | | − if (location_changed < 0) |
| 275 | | − goto fail; |
| 315 | + if (location_changed < 0) { |
| 316 | + if (!http_should_reconnect(s, location_changed) || |
| 317 | + reconnect_delay > s->reconnect_delay_max) |
| 318 | + goto fail; |
| 319 | + |
| 320 | + av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s).\n", off, reconnect_delay); |
| 321 | + location_changed = ff_network_sleep_interruptible(1000U * 1000 * reconnect_delay, &h->interrupt_callback); |
| 322 | + if (location_changed != AVERROR(ETIMEDOUT)) |
| 323 | + goto fail; |
| 324 | + reconnect_delay = 1 + 2 * reconnect_delay; |
| 325 | + |
| 326 | + /* restore the offset (http_connect resets it) */ |
| 327 | + s->off = off; |
| 328 | + |
| 329 | + ffurl_closep(&s->hd); |
| 330 | + goto redo; |
| 331 | + } |
| 276 | 332 | |
| 277 | 333 | attempts++; |
| 278 | 334 | if (s->http_code == 401) { |