2002-09-03 13:52:59 +02:00
|
|
|
/***************************************************************************
|
2000-10-02 08:30:40 +02:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2003-01-16 22:08:12 +01:00
|
|
|
* Copyright (C) 1998 - 2003, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2000-10-02 08:30:40 +02:00
|
|
|
*
|
2002-09-03 13:52:59 +02:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
2001-01-03 10:29:33 +01:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 13:52:59 +02:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2000-10-02 08:30:40 +02:00
|
|
|
*
|
2001-01-03 10:29:33 +01:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
2000-10-02 08:30:40 +02:00
|
|
|
*
|
2001-01-03 10:29:33 +01:00
|
|
|
* $Id$
|
2002-09-03 13:52:59 +02:00
|
|
|
***************************************************************************/
|
2000-10-02 08:30:40 +02:00
|
|
|
|
2002-05-10 16:37:39 +02:00
|
|
|
#include "setup.h"
|
|
|
|
|
2000-10-02 08:30:40 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-05-17 09:57:13 +02:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2002-05-10 16:37:39 +02:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2000-10-02 08:30:40 +02:00
|
|
|
#include <curl/curl.h>
|
2000-11-21 10:38:41 +01:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
|
2000-10-02 08:30:40 +02:00
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
#include "writeout.h"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
VAR_NONE, /* must be the first */
|
|
|
|
VAR_TOTAL_TIME,
|
|
|
|
VAR_NAMELOOKUP_TIME,
|
|
|
|
VAR_CONNECT_TIME,
|
|
|
|
VAR_PRETRANSFER_TIME,
|
2001-11-20 16:00:50 +01:00
|
|
|
VAR_STARTTRANSFER_TIME,
|
2000-10-02 08:30:40 +02:00
|
|
|
VAR_SIZE_DOWNLOAD,
|
|
|
|
VAR_SIZE_UPLOAD,
|
|
|
|
VAR_SPEED_DOWNLOAD,
|
|
|
|
VAR_SPEED_UPLOAD,
|
|
|
|
VAR_HTTP_CODE,
|
2000-10-04 15:08:17 +02:00
|
|
|
VAR_HEADER_SIZE,
|
|
|
|
VAR_REQUEST_SIZE,
|
2000-10-02 08:30:40 +02:00
|
|
|
VAR_EFFECTIVE_URL,
|
2002-02-25 08:40:49 +01:00
|
|
|
VAR_CONTENT_TYPE,
|
2000-10-02 08:30:40 +02:00
|
|
|
VAR_NUM_OF_VARS /* must be the last */
|
|
|
|
} replaceid;
|
|
|
|
|
|
|
|
struct variable {
|
2001-08-14 11:16:46 +02:00
|
|
|
const char *name;
|
2000-10-02 08:30:40 +02:00
|
|
|
replaceid id;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct variable replacements[]={
|
|
|
|
{"url_effective", VAR_EFFECTIVE_URL},
|
|
|
|
{"http_code", VAR_HTTP_CODE},
|
|
|
|
{"time_total", VAR_TOTAL_TIME},
|
|
|
|
{"time_namelookup", VAR_NAMELOOKUP_TIME},
|
|
|
|
{"time_connect", VAR_CONNECT_TIME},
|
|
|
|
{"time_pretransfer", VAR_PRETRANSFER_TIME},
|
2001-11-20 16:00:50 +01:00
|
|
|
{"time_starttransfer", VAR_STARTTRANSFER_TIME},
|
2000-10-04 15:08:17 +02:00
|
|
|
{"size_header", VAR_HEADER_SIZE},
|
|
|
|
{"size_request", VAR_REQUEST_SIZE},
|
2000-10-02 08:30:40 +02:00
|
|
|
{"size_download", VAR_SIZE_DOWNLOAD},
|
|
|
|
{"size_upload", VAR_SIZE_UPLOAD},
|
|
|
|
{"speed_download", VAR_SPEED_DOWNLOAD},
|
|
|
|
{"speed_upload", VAR_SPEED_UPLOAD},
|
2002-02-25 08:40:49 +01:00
|
|
|
{"content_type", VAR_CONTENT_TYPE},
|
2001-08-14 11:16:46 +02:00
|
|
|
{NULL, 0}
|
2000-10-02 08:30:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void ourWriteOut(CURL *curl, char *writeinfo)
|
|
|
|
{
|
|
|
|
FILE *stream = stdout;
|
|
|
|
char *ptr=writeinfo;
|
|
|
|
char *stringp;
|
|
|
|
long longinfo;
|
|
|
|
double doubleinfo;
|
|
|
|
|
|
|
|
while(*ptr) {
|
|
|
|
if('%' == *ptr) {
|
|
|
|
if('%' == ptr[1]) {
|
|
|
|
/* an escaped %-letter */
|
|
|
|
fputc('%', stream);
|
|
|
|
ptr+=2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* this is meant as a variable to output */
|
|
|
|
char *end;
|
|
|
|
char keepit;
|
|
|
|
int i;
|
|
|
|
if(('{' == ptr[1]) && (end=strchr(ptr, '}'))) {
|
|
|
|
ptr+=2; /* pass the % and the { */
|
|
|
|
keepit=*end;
|
|
|
|
*end=0; /* zero terminate */
|
|
|
|
for(i=0; replacements[i].name; i++) {
|
2003-02-28 13:20:08 +01:00
|
|
|
if(curl_strequal(ptr, replacements[i].name)) {
|
2000-10-02 08:30:40 +02:00
|
|
|
switch(replacements[i].id) {
|
|
|
|
case VAR_EFFECTIVE_URL:
|
2002-07-30 13:07:59 +02:00
|
|
|
if((CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
|
|
|
|
&& stringp)
|
2000-10-02 08:30:40 +02:00
|
|
|
fputs(stringp, stream);
|
|
|
|
break;
|
|
|
|
case VAR_HTTP_CODE:
|
|
|
|
if(CURLE_OK ==
|
2003-08-20 17:42:24 +02:00
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
|
2000-10-02 08:30:40 +02:00
|
|
|
fprintf(stream, "%03d", longinfo);
|
|
|
|
break;
|
2000-10-04 15:08:17 +02:00
|
|
|
case VAR_HEADER_SIZE:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
|
|
|
|
fprintf(stream, "%d", longinfo);
|
|
|
|
break;
|
|
|
|
case VAR_REQUEST_SIZE:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
|
|
|
|
fprintf(stream, "%d", longinfo);
|
|
|
|
break;
|
2000-10-02 08:30:40 +02:00
|
|
|
case VAR_TOTAL_TIME:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
|
|
|
case VAR_NAMELOOKUP_TIME:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
|
|
|
|
&doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
|
|
|
case VAR_CONNECT_TIME:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
|
|
|
case VAR_PRETRANSFER_TIME:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
2001-11-20 16:00:50 +01:00
|
|
|
case VAR_STARTTRANSFER_TIME:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
2000-10-02 08:30:40 +02:00
|
|
|
case VAR_SIZE_UPLOAD:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
|
2001-04-19 13:24:29 +02:00
|
|
|
fprintf(stream, "%.0f", doubleinfo);
|
2000-10-02 08:30:40 +02:00
|
|
|
break;
|
|
|
|
case VAR_SIZE_DOWNLOAD:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &doubleinfo))
|
2001-04-19 13:24:29 +02:00
|
|
|
fprintf(stream, "%.0f", doubleinfo);
|
2000-10-02 08:30:40 +02:00
|
|
|
break;
|
|
|
|
case VAR_SPEED_DOWNLOAD:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
|
|
|
case VAR_SPEED_UPLOAD:
|
|
|
|
if(CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
|
|
|
|
fprintf(stream, "%.3f", doubleinfo);
|
|
|
|
break;
|
2002-02-25 08:40:49 +01:00
|
|
|
case VAR_CONTENT_TYPE:
|
2002-07-30 13:07:59 +02:00
|
|
|
if((CURLE_OK ==
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
|
|
|
|
&& stringp)
|
|
|
|
fputs(stringp, stream);
|
|
|
|
break;
|
2000-11-21 10:38:41 +01:00
|
|
|
default:
|
|
|
|
break;
|
2000-10-02 08:30:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr=end+1; /* pass the end */
|
|
|
|
*end = keepit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* illegal syntax, then just output the characters that are used */
|
|
|
|
fputc('%', stream);
|
|
|
|
fputc(ptr[1], stream);
|
|
|
|
ptr+=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if('\\' == *ptr) {
|
|
|
|
switch(ptr[1]) {
|
|
|
|
case 'r':
|
|
|
|
fputc('\r', stream);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
fputc('\n', stream);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
fputc('\t', stream);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* unknown, just output this */
|
|
|
|
fputc(*ptr, stream);
|
|
|
|
fputc(ptr[1], stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr+=2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fputc(*ptr, stream);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|