📄 http_basic.c
字号:
* content-range-spec = byte-content-range-spec * byte-content-range-spec = bytes-unit SP * byte-range-resp-spec "/" * ( instance-length | "*" ) * * byte-range-resp-spec = (first-byte-pos "-" last-byte-pos) * | "*" * instance-length = 1*DIGIT * @endcode * *//**@ingroup http_content_range * @typedef typedef struct http_content_range_s http_content_range_t; * * The structure #http_content_range_t contains representation of * @b Content-Range header. * * The #http_content_range_t is defined as follows: * @code * typedef struct { * msg_common_t cr_common[1]; * msg_error_t *cr_next; * off_t cr_first; // First-byte-pos * off_t cr_last; // Last-byte-pos * off_t cr_length; // Instance-length * } http_content_range_t; * @endcode */int http_content_range_d(su_home_t *home, http_header_t *h, char *s, int slen){ http_content_range_t *cr = h->sh_content_range; if (strncasecmp(s, "bytes", 5)) return -1; s += 5; skip_lws(&s); if (s[0] == '*') { cr->cr_first = cr->cr_last = (http_off_t)-1; s++; skip_lws(&s); } else { if (msg_delta_d((char const **)&s, &cr->cr_first) < 0) return -1; if (s[0] != '-') return -1; s++; skip_lws(&s); if (msg_delta_d((char const **)&s, &cr->cr_last) < 0) return -1; } if (s[0] != '/') return -1; s++; skip_lws(&s); if (s[0] == '*') { cr->cr_length = (http_off_t)-1; s++; skip_lws(&s); } else { if (msg_delta_d((char const **)&s, &cr->cr_length) < 0) return -1; } return s[0] ? -1 : 0;}int http_content_range_e(char b[], int bsiz, http_header_t const *h, int f){ http_content_range_t const *cr = h->sh_content_range; if (cr->cr_first == (http_off_t)-1) { if (cr->cr_length == (http_off_t)-1) return snprintf(b, bsiz, "bytes */*"); else return snprintf(b, bsiz, "bytes */%lu", cr->cr_length); } else { if (cr->cr_length == (http_off_t)-1) return snprintf(b, bsiz, "bytes %lu-%lu/*", cr->cr_first, cr->cr_last); else return snprintf(b, bsiz, "bytes %lu-%lu/%lu", cr->cr_first, cr->cr_last, cr->cr_length); }}msg_hclass_t http_content_range_class[] =HTTP_HEADER_CLASS(content_range, "Content-Range", cr_common, single, default);/* ====================================================================== *//**@HTTP_HEADER http_content_type Content-Type header. * * We use MIME Content-Type header. *//* ====================================================================== *//**@HTTP_HEADER http_date Date header. * * The Date header field reflects the time when the request or response was * first sent. Its syntax is defined in [H14.18] as * follows: * * @code * Date = "Date" HCOLON HTTP-date * HTTP-date = rfc1123-date * rfc1123-date = wkday "," SP date1 SP time SP "GMT" * date1 = 2DIGIT SP month SP 4DIGIT * ; day month year (e.g., 02 Jun 1982) * time = 2DIGIT ":" 2DIGIT ":" 2DIGIT * ; 00:00:00 - 23:59:59 * wkday = "Mon" / "Tue" / "Wed" * / "Thu" / "Fri" / "Sat" / "Sun" * month = "Jan" / "Feb" / "Mar" / "Apr" * / "May" / "Jun" / "Jul" / "Aug" * / "Sep" / "Oct" / "Nov" / "Dec" * @endcode * *//**@ingroup http_date * @typedef typedef struct http_date_s http_date_t; * * The structure #http_date_t contains representation of @b Date header. * * The #http_date_t is defined as follows: * @code * typedef struct { * msg_common_t d_common[1]; // Common fragment info * msg_error_t *d_next; // Link to next (dummy) * http_time_t d_time; // Seconds since Jan 1, 1900 * } http_date_t; * @endcode */int http_date_d(su_home_t *home, http_header_t *h, char *s, int slen){ http_date_t *date = h->sh_date; if (msg_date_d((char const **)&s, &date->d_time) < 0 || *s) return -1; else return 0;}int http_date_e(char b[], int bsiz, http_header_t const *h, int f){ http_date_t const *date = h->sh_date; return msg_date_e(b, bsiz, date->d_time);}/**@ingroup http_date * @brief Create an @b Date header object. * * The function http_date_create() creates a Date header object with the * date @a date. If @date is 0, current time (as returned by msg_now()) is * used. * * @param home memory home * @param date date expressed as seconds since Mon, 01 Jan 1900 00:00:00 * * @return * The function http_date_create() returns a pointer to newly created * @b Date header object when successful, or NULL upon an error. */http_date_t *http_date_create(su_home_t *home, http_time_t date){ http_header_t *h = msg_header_alloc(home, http_date_class, 0); if (h) { if (date == 0) date = msg_now(); h->sh_date->d_time = date; } return h->sh_date;}msg_hclass_t http_date_class[] =HTTP_HEADER_CLASS(date, "Date", d_common, single, default);/* ====================================================================== *//**@HTTP_HEADER http_etag ETag header. */#define http_etag_d msg_generic_d#define http_etag_e msg_generic_emsg_hclass_t http_etag_class[] =HTTP_HEADER_CLASS_G(etag, "ETag", single);/* ====================================================================== *//**@HTTP_HEADER http_expect Expect header. */#define http_expect_d msg_generic_d#define http_expect_e msg_generic_emsg_hclass_t http_expect_class[] =HTTP_HEADER_CLASS_G(expect, "Expect", single);/* ====================================================================== *//**@HTTP_HEADER http_expires Expires header. * * The Expires header field gives the date and time after which the message * content expires. Its syntax is defined in RFC 1428 section 14.21 as * follows: * * @code * Expires = "Expires:" HTTP-date * @endcode * *//**@ingroup http_expires * @typedef typedef struct http_expires_s http_expires_t; * * The structure #http_expires_t contains representation of @b Expires * header. * * The #http_expires_t is defined as follows: * @code * typedef struct { * msg_common_t d_common[1]; // Common fragment info * msg_error_t *d_next; // Link to next (dummy) * http_time_t d_time; // Seconds since Jan 1, 1900 * } http_expires_t; * @endcode */#define http_expires_d http_date_d#define http_expires_e http_date_emsg_hclass_t http_expires_class[] =HTTP_HEADER_CLASS(expires, "Expires", d_common, single, default);/* ====================================================================== *//**@HTTP_HEADER http_from From header. * * @code * From = "From" ":" mailbox * @endcode */#define http_from_d msg_generic_d#define http_from_e msg_generic_emsg_hclass_t http_from_class[] =HTTP_HEADER_CLASS_G(from, "From", single);/* ====================================================================== *//**@HTTP_HEADER http_host Host header. * * @code * Host = "Host" ":" host [ ":" port ] * @endcode *//** Parse Host header */int http_host_d(su_home_t *home, http_header_t *h, char *s, int slen){ http_host_t *host = h->sh_host; if (msg_hostport_d(&s, &host->h_host, &host->h_port) < 0 || *s) return -1; return 0;}/** Print Host header */int http_host_e(char b[], int bsiz, http_header_t const *h, int flags){ char *b0 = b, *end = b + bsiz; MSG_STRING_E(b, end, h->sh_host->h_host); if (h->sh_host->h_port) { MSG_CHAR_E(b, end, ':'); MSG_STRING_E(b, end, h->sh_host->h_port); } return b - b0;}/** Extra size of a http_host_t object. */staticint http_host_dup_xtra(http_header_t const *h, int offset){ offset += MSG_STRING_SIZE(h->sh_host->h_host); offset += MSG_STRING_SIZE(h->sh_host->h_port); return offset;}/** Duplicate one Host header. */staticchar *http_host_dup_one(http_header_t *dst, http_header_t const *src, char *b, int xtra){ http_host_t *h = dst->sh_host; http_host_t const *o = src->sh_host; char *end = b + xtra; MSG_STRING_DUP(b, h->h_host, o->h_host); MSG_STRING_DUP(b, h->h_port, o->h_port); assert(b <= end); return b;}/**Create a Host object. */http_host_t *http_host_create(su_home_t *home, char const *host, char const *port){ http_host_t h[1]; http_host_init(h); h->h_host = host, h->h_port = port; if (host) { return http_host_dup(home, h); } else return NULL;}msg_hclass_t http_host_class[] =HTTP_HEADER_CLASS(host, "Host", h_common, single, host);/* ====================================================================== *//**@HTTP_HEADER http_if_match If-Match header. */#define http_if_match_d msg_list_d#define http_if_match_e msg_list_emsg_hclass_t http_if_match_class[] =HTTP_HEADER_CLASS_LIST(if_match, "If-Match", list);/* ====================================================================== *//**@HTTP_HEADER http_if_modified_since If-Modified-Since header. * * The If-Modified-Since header field The If-Modified-Since request-header * field is used with a method to make it conditional: if the requested * variant has not been modified since the time specified in this field, an * entity will not be returned from the server; instead, a 304 (not * modified) response will be returned without any message-body. Its syntax * is defined in RFC 2616 secion 14.25 as follows: * * @code * If-Modified-Since = "If-Modified-Since" ":" HTTP-date * @endcode * *//**@ingroup http_if_modified_since * @typedef typedef struct http_if_modified_since_s http_if_modified_since_t; * * The structure #http_if_modified_since_t contains representation of * @b If-Modified-Since header. * * The #http_if_modified_since_t is defined as follows: * @code * typedef struct { * msg_common_t d_common[1]; // Common fragment info * msg_error_t *d_next; // Link to next (dummy) * http_time_t d_time; // Seconds since Jan 1, 1900 * } http_if_modified_since_t; * @endcode */#define http_if_modified_since_d http_date_d#define http_if_modified_since_e http_date_emsg_hclass_t http_if_modified_since_class[] =HTTP_HEADER_CLASS(if_modified_since, "If-Modified-Since", d_common, single, default);/* ====================================================================== *//**@HTTP_HEADER http_if_none_match If-None-Match header. */#define http_if_none_match_d msg_list_d#define http_if_none_match_e msg_list_emsg_hclass_t http_if_none_match_class[] =HTTP_HEADER_CLASS_LIST(if_none_match, "If-None-Match", list);/* ====================================================================== *//**@HTTP_HEADER http_if_range If-Range header. * * The @b If-Range header is used when a client has a partial copy of an * entity in its cache, and wishes to have an up-to-date copy of the entire * entity. Informally, its meaning is `if the entity is unchanged, send * me the part(s) that I am missing; otherwise, send me the entire new * entity'. Its syntax is defined in RFC 2616 as follows: * * @code * If-Range = "If-Range" ":" ( entity-tag / HTTP-date ) * @endcode */ /** Parse If-Range header */int http_if_range_d(su_home_t *home, http_header_t *h, char *s, int slen){ http_if_range_t *ifr = (http_if_range_t *)h; if (s[0] == '"' || strncasecmp(s, "W/\"", 3) == 0) { ifr->ifr_tag = s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -