void http_request(http_t *ctx, const char *verb, const char *host, uint16_t port, const char *uri) { http_log(ctx, "%s: host=\"%s\" uri=\"%s\"", __func__, host, uri); http_send_str(ctx, verb); http_send_str(ctx, " "); http_send_str(ctx, uri); http_send_line(ctx, " HTTP/1.1"); http_send_str(ctx, "Host: "); http_send_str(ctx, host); if (port != HTTP_DEFAULT_PORT) { char buf[32], *p = buf; size_t size = sizeof buf; p = append_char(p, &size, ':'); p = append_uint(p, &size, port); http_send_str(ctx, buf); } http_send_str(ctx, "\r\n"); http_send_str(ctx, http_useragent_header); http_send_line(ctx, http_connection_header(ctx)); http_send_extra_headers(ctx); }
int http_send_status_line(http_t *ctx, unsigned int code, const char *msg) { char buf[256], *p = buf; size_t size = sizeof buf; RUNTIME_ASSERT(code >= 100 && code <= 999); ACCLOG_SET(ctx->acclog, code, code); p = append_string(p, &size, "HTTP/1.1 "); p = append_uint(p, &size, code); p = append_char(p, &size, ' '); p = append_string(p, &size, msg); #if 0 DBUG("Sending HTTP response \"%s\"", buf); #endif p = APPEND_CRLF(p, &size); p = APPEND_STATIC_CHARS(p, &size, "Date: "); p = append_date(p, &size, compat_mono_time(NULL)); p = APPEND_CRLF(p, &size); if ( 0 != http_send(ctx, buf, sizeof buf - size) || 0 != http_send_str(ctx, http_server_header) ) return -1; return 0; }
/* * Utility functions to format signed/unsigned integers and append * them to an archive_string. */ static void append_uint(struct archive_string *as, uintmax_t d, unsigned base) { static const char *digits = "0123456789abcdef"; if (d >= base) append_uint(as, d/base, base); archive_strappend_char(as, digits[d % base]); }
static void append_int(struct archive_string *as, intmax_t d, unsigned base) { if (d < 0) { archive_strappend_char(as, '-'); d = -d; } append_uint(as, d, base); }
static void append_int(struct archive_string *as, intmax_t d, unsigned base) { uintmax_t ud; if (d < 0) { archive_strappend_char(as, '-'); ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d); } else ud = d; append_uint(as, ud, base); }
/* * Like 'vsprintf', but ensures the target is big enough, resizing if * necessary. */ void archive_string_vsprintf(struct archive_string *as, const char *fmt, va_list ap) { char long_flag; intmax_t s; /* Signed integer temp. */ uintmax_t u; /* Unsigned integer temp. */ const char *p, *p2; const wchar_t *pw; if (archive_string_ensure(as, 64) == NULL) __archive_errx(1, "Out of memory"); if (fmt == NULL) { as->s[0] = 0; return; } for (p = fmt; *p != '\0'; p++) { const char *saved_p = p; if (*p != '%') { archive_strappend_char(as, *p); continue; } p++; long_flag = '\0'; switch(*p) { case 'j': case 'l': case 'z': long_flag = *p; p++; break; } switch (*p) { case '%': archive_strappend_char(as, '%'); break; case 'c': s = va_arg(ap, int); archive_strappend_char(as, (char)s); break; case 'd': switch(long_flag) { case 'j': s = va_arg(ap, intmax_t); break; case 'l': s = va_arg(ap, long); break; case 'z': s = va_arg(ap, ssize_t); break; default: s = va_arg(ap, int); break; } append_int(as, s, 10); break; case 's': switch(long_flag) { case 'l': pw = va_arg(ap, wchar_t *); if (pw == NULL) pw = L"(null)"; if (archive_string_append_from_wcs(as, pw, wcslen(pw)) != 0 && errno == ENOMEM) __archive_errx(1, "Out of memory"); break; default: p2 = va_arg(ap, char *); if (p2 == NULL) p2 = "(null)"; archive_strcat(as, p2); break; } break; case 'S': pw = va_arg(ap, wchar_t *); if (pw == NULL) pw = L"(null)"; if (archive_string_append_from_wcs(as, pw, wcslen(pw)) != 0 && errno == ENOMEM) __archive_errx(1, "Out of memory"); break; case 'o': case 'u': case 'x': case 'X': /* Common handling for unsigned integer formats. */ switch(long_flag) { case 'j': u = va_arg(ap, uintmax_t); break; case 'l': u = va_arg(ap, unsigned long); break; case 'z': u = va_arg(ap, size_t); break; default: u = va_arg(ap, unsigned int); break; } /* Format it in the correct base. */ switch (*p) { case 'o': append_uint(as, u, 8); break; case 'u': append_uint(as, u, 10); break; default: append_uint(as, u, 16); break; } break; default: /* Rewind and print the initial '%' literally. */ p = saved_p; archive_strappend_char(as, *p); } } }