/* kenchunk.c * チャンク形式のデータの作成 * ・引数でコンテンツタイプを指定しているが、あまり意味はない。 * ・チャンクサイズはランダム */ #define USAGE "Usage: kenchunk " #include #include #include "apr_general.h" #include "apr_file_io.h" #include "apr_strings.h" #include "mystab.h" int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) { apr_status_t rv; char error_buf[256]; char *ctype=NULL; apr_file_t *fpin = NULL; apr_file_t *fpout = NULL; char *infilename = NULL; char *outfilename = NULL; if (ac < 4) { apr_file_printf(astderr, "%s", USAGE); goto _ERROR_; } infilename = av[1]; ctype = av[2]; outfilename = av[3]; if (APR_SUCCESS != (rv = apr_file_open(&fpin, infilename, APR_FOPEN_READ|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); goto _ERROR_; } if (APR_SUCCESS != (rv = apr_file_open(&fpout, outfilename, APR_FOPEN_WRITE|APR_FOPEN_CREATE|APR_FOPEN_TRUNCATE|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); goto _ERROR_; } /* ヘッダの書き込み */ if (fpout && ctype) { apr_file_printf(fpout, "Transfer-Encoding: chunked\r\n"); apr_file_printf(fpout, "Content-Type: %s\r\n\r\n", ctype); } srand((unsigned int)time(0)); /* チャンク形式での書き込み */ if (fpin && fpout && ctype) { char buf[1024]; apr_size_t len = 0; while (APR_EOF != (rv=apr_file_eof(fpin))) { len = (apr_size_t)(rand()%1023); if (APR_SUCCESS != (rv=apr_file_read(fpin, buf, &len))) { if (APR_EOF == rv) { break; } apr_file_printf(astderr, "ERROR: apr_file_read: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); goto _ERROR_; } /* チャンクサイズの書き込み */ apr_file_printf(fpout, "%x\r\n", len); /* チャンクデータの書き込み */ apr_file_write(fpout, buf, &len); /* CRLF の書き込み */ apr_file_printf(fpout, "\r\n"); } /* ラストチャンクと CRLF の書き込み */ apr_file_printf(fpout, "0\r\n\r\n"); } if (fpin) { apr_file_close(fpin); fpin = NULL; } if (fpout) { apr_file_close(fpout); fpout = NULL; } apr_file_printf(astdout, "\ndone.\n"); return 0; /* 正常終了 */ _ERROR_: if (fpin) { apr_file_close(fpin); fpin = NULL; } if (fpout) { apr_file_close(fpout); fpout = NULL; } apr_file_printf(astderr, "\nfailed.\n"); return 1; /* 異常終了 */ }