用C自撸apache简易模块,搭建图片处理服务器。

写C是个撸sir

/*
** mod_acthumb.c -- Apache sample acthumb module
** [Autogenerated via ``apxs -n acthumb -g'']
**
** To play with this sample module first compile it into a
** DSO file and install it into Apache's modules directory
** by running:
**
** $ apxs -c -i mod_acthumb.c
**
** Then activate it in Apache's httpd.conf file for instance
** for the URL /acthumb in as follows:
**
** # httpd.conf
** LoadModule acthumb_module modules/mod_acthumb.so
** <Location /acthumb>
** SetHandler acthumb
** </Location>
**
** Then after restarting Apache via
**
** $ apachectl restart
**
** you immediately can request the URL /acthumb and watch for the
** output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/acthumb
**
** The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_acthumb.c
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "wand/MagickWand.h"
/* The sample content handler */
static int acthumb_handler(request_rec *r)
{
if (strcmp(r->handler, "application/acjpg")) {
return DECLINED;
}
r->content_type = "image/jpeg";
if (!r->header_only){
char src[300];
char dest[300];
char generatepic[300];
memset(dest, '\0', sizeof(dest));
strcpy(src,r->filename); //1.jpg.acjpg
strncpy(dest, src, strlen(src)-6); //.acjpg six letters 1.jpg
char *widths = strtok(r->args,"@"); //800
char *heights = strtok(NULL,"@"); //700
int width=atoi(widths);
int height = atoi(heights);
if((width==50 && height==50) || (width==100 && height==100) || (width==300 && height==300) || (width==500 && height==500) || (width==640 && height==640) || (width==640 && height > 200)){ strncpy(generatepic, dest, strlen(src)-10); //10 .jpg.acjpg fetch 1
char *fileprefix = strtok(dest,".");
char *filesuffix = strtok(NULL,"."); char *POSFILETYPE[]={"jpg","png","gif","jpeg"};
int i;
int hasfiletype=0;
for( i = 0; i < 4; i=i+1){
if(strcmp(POSFILETYPE[i],filesuffix)==0){
hasfiletype = 1;
}
} if(hasfiletype==0){
r->status=HTTP_NOT_FOUND;
ap_send_error_response(r, 0);
} strcat(generatepic,"_");
strcat(generatepic,widths);
strcat(generatepic,"_");
strcat(generatepic,heights);
strcat(generatepic,".");
strcat(generatepic,filesuffix);
strcat(dest,".");
strcat(dest,filesuffix); if((access(generatepic,F_OK))==-1){
MagickBooleanType status;
MagickWand *magick_wand;
MagickWandGenesis();
magick_wand=NewMagickWand();
status=MagickReadImage(magick_wand,dest);
//while (MagickNextImage(magick_wand) != MagickFalse)
MagickResizeImage(magick_wand,width,height,LanczosFilter,1); status=MagickWriteImages(magick_wand,generatepic,MagickTrue); magick_wand=DestroyMagickWand(magick_wand);
MagickWand*();
} apr_file_t *file;
apr_size_t sent;
apr_finfo_t file_info;
apr_status_t rc; apr_stat(&file_info, generatepic, APR_FINFO_MIN, r->pool); rc = apr_file_open(&file, generatepic, APR_READ, APR_OS_DEFAULT,
r->pool); if (rc == APR_SUCCESS) {
ap_send_fd(file, r, 0, (apr_size_t)file_info.size, &sent);
apr_file_close(file);
}
else {
ap_rputs(generatepic,r);
}
}else{
r->status=HTTP_NOT_FOUND;
ap_send_error_response(r, 0);
}
}
return OK;
} static void acthumb_register_hooks(apr_pool_t *p)
{
ap_hook_handler(acthumb_handler, NULL, NULL, APR_HOOK_MIDDLE);
} /* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA acthumb_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
acthumb_register_hooks /* register hooks */
};
上一篇:POJ1276:Cash Machine(多重背包)


下一篇:Cash Machine(多重背包二进制转换)