/*
 * HTTP definitions
 * Copyright (c) 2010 Josh Allmann
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVFORMAT_HTTP_H
#define AVFORMAT_HTTP_H

#include "url.h"

#include "libavutil/avstring.h"
#include "avformat.h"
#include <unistd.h>
#include "internal.h"
#include "network.h"

#include "os_support.h"
#include "httpauth.h"
#include "url.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"


/* XXX: POST protocol is not completely implemented because ffmpeg uses
   only a subset of it. */

/* used for protocol handling */
#define BUFFER_SIZE (1024 * 12)
#define MAX_REDIRECTS 8

typedef struct {
    const AVClass *class;
    URLContext *hd;
    unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
    int line_count;
    int http_code;
    int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
    char *content_type;
    char *user_agent;
    int64_t off, filesize;
    char *location;
    HTTPAuthState auth_state;
    HTTPAuthState proxy_auth_state;
    char *headers;
    int willclose;          /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
    int seekable;           /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
    int chunked_post;
    int end_chunked_post;   /**< A flag which indicates if the end of chunked encoding has been sent. */
    int end_header;         /**< A flag which indicates we have finished to read POST reply. */
    int multiple_requests;  /**< A flag which indicates if we use persistent connections. */
    int max_reuse; ///<max number of times this connection can be reused
    int64_t reuse_timeout; ///<time after which a new connection should be created
    uint8_t *post_data;
    time_t server_time; ///<the server time
    int post_datalen;
    int use_tls; ///<set to 1 when tls is used as lower protocol
    int is_akamai;
    int rw_timeout;
    int conn_timeout;
    char *mime_type;
    char *cookies;          ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
    int succeed_on_http_error; ///<if set to 1, return a valid context for non 200 http codes
} HTTPContext;

/**
 * Initialize the authentication state based on another HTTP URLContext.
 * This can be used to pre-initialize the authentication parameters if
 * they are known beforehand, to avoid having to do an initial failing
 * request just to get the parameters.
 *
 * @param dest URL context whose authentication state gets updated
 * @param src URL context whose authentication state gets copied
 */
void ff_http_init_auth_state(URLContext *dest, const URLContext *src);

/**
 * Send a new HTTP request, reusing the old connection.
 *
 * @param h pointer to the resource
 * @param uri uri used to perform the request
 * @return a negative value if an error condition occurred, 0
 * otherwise
 */
int ff_http_do_new_request(URLContext *h, const char *uri);

/**
 * Retrieves the IP address of the server reached for this connection
 */
const char* ff_http_get_ip_address(URLContext *h);

extern int http_latest_error_code;

#endif /* AVFORMAT_HTTP_H */
