• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 47, 文章 - 10, 評論 - 8, 引用 - 0
            數據加載中……

            Apache模塊 開發實例(轉)

             

              1/**
              2* Copyright 2003 Tom, Inc. All rights reserved.
              3*
              4* Description: Apache模塊 取用戶圖片
              5* FileName: useravatar.c
              6*
              7* @Author sam
              8* <net.eagle@163.com>
              9
             10* /home/webadm/bin/apxs -c -o mod_useravatar.so useravatar.c
             11*
             12* <Location /avatar>
             13* SetHandler get_useravatar
             14* </Location>
             15*
             16* LoadModule useravatar_module  libexec/mod_useravatar.so
             17* AddModule useravatar.c
             18*
             19*
             20* 修改記錄
             21* 修改 const char *DOMAIN ="http://avatar.tom.com/public&quot;   為  http://avatarphoto.tom.com/public    2004-4-1
             22* 修改 const char *DEFAULT_WEB_AVATAR = "http://avatar.tom.com/public/default.gif"
             23* 為  http://avatarphoto.tom.com/public/default.gif    2004-4-1
             24*
             25* 為了可以讓圖片在 image cache server中及時更新 必須得每次換圖片名,所以得在每個目錄下寫一個username.txt 不標記用戶
             26* 的新名稱.  2004-4-22
             27*
             28*/

             29
             30#include "httpd.h"
             31#include "http_config.h"
             32#include "http_core.h"
             33#include "http_request.h"
             34
             35
             36#include <stdio.h>
             37
             38#include <sys/types.h>
             39#include <sys/stat.h>
             40#include <unistd.h>
             41
             42
             43//默認Avatar圖片地址
             44const char *DEFAULT_AVATAR = "/www/web/public/default.gif";
             45const char *ROOT_PATH      = "/www/web/public";
             46const int   MODS = 128;
             47//const char *DOMAIN         = "http://avatarphoto.tom.com/public";
             48//const char *DEFAULT_WEB_AVATAR = "http://avatarphoto.tom.com/public/default.gif";
             49
             50const char *DOMAIN         = "http://avatartest.tom.com/public";
             51const char *DEFAULT_WEB_AVATAR = "http://avatartest.tom.com/public/default.gif";
             52
             53module MODULE_VAR_EXPORT useravatar_module;
             54
             55static void redirect(request_rec *r, const char *path);
             56static char *open_avatars_txt(request_rec *r, char *username);
             57static char *get_file_path(char *username, char *type);
             58static char *get_web_path(char *username);
             59static void log(char *msg);
             60static int checkfile(char *path);
             61
             62
             63static int useravatar_handler(request_rec *r)
             64{
             65        //圖片目錄
             66        char *path;
             67
             68        //用戶name
             69        char *username;
             70
             71        //取url 后面的username
             72        username = strrchr(&(r->uri[1]), '/');
             73
             74        //取默認圖片
             75        if(!username)
             76                redirect(r, DEFAULT_WEB_AVATAR);
             77
             78        //去掉 '/' 符號
             79        username++;
             80
             81        //取用戶形象新文件名
             82        path = open_avatars_txt(r, username);
             83
             84        redirect(r, path);
             85
             86        /*
             87        if(1 == checkfile(path))
             88        {
             89                path = get_web_path(username);
             90
             91                if(path) 
             92                        redirect(r, path);
             93                else
             94                        redirect(r, DEFAULT_WEB_AVATAR);
             95
             96        }else{
             97                redirect(r, DEFAULT_WEB_AVATAR);
             98        }
             99        */

            100
            101        //r->content_type = CONTENT_TYPE;
            102        //ap_send_http_header(r);
            103
            104        return REDIRECT;
            105}

            106
            107
            108/**
            109*  轉向
            110*/

            111
            112static void redirect(request_rec *r, const char *path)
            113{
            114        ap_table_setn(r->headers_out, "Http""302");
            115        ap_table_setn(r->headers_out, "Location", path);
            116}

            117
            118
            119/**
            120
            121* usage: 檢查圖片是否存在
            122*
            123* @param  path 要檢查的文件path
            124*
            125* @return  0 文件不存在  1 存在
            126*
            127*/

            128static int checkfile(char *path)
            129{
            130        FILE * fp;
            131        fp = fopen(path,"rb");
            132        if(!fp) return 0;
            133        fclose(fp);
            134
            135        return 1;
            136}

            137
            138/**
            139
            140* usage: 讀取標記形象的txt 記錄.
            141*
            142* @param 
            143*
            144* @return 返回新文件名的名字
            145*
            146*/

            147static char *open_avatars_txt(request_rec *r, char *username)
            148{
            149        char *buf = NULL;
            150        FILE * fp;
            151
            152        //存放返回的path
            153        buf = (char *)malloc(100);
            154
            155        fp = fopen(get_file_path(username, "txt"),"rb");
            156
            157        if(!fp)
            158        {
            159                //取不到 記錄文件名的文件,則取 username 為了保證跟以前的結構兼容
            160                //buf = get_file_path(username, "gif");
            161                buf = username;
            162        }
            else{
            163                //取username.txt 文件中的記錄新圖片的文件名
            164                if(fgets(buf, 100, fp))
            165                {
            166                        buf[strlen(buf)] = '';
            167                }

            168
            169                fclose(fp);
            170        }

            171
            172        //檢查圖片是否存在,否則取默認圖片 并返回 web 方式的地址
            173        log(get_file_path(buf, "gif"));
            174
            175        if(0 == checkfile(get_file_path(buf, "gif")))
            176                return (char *)DEFAULT_WEB_AVATAR;
            177        else
            178                return get_web_path(buf);
            179}

            180
            181static void log(char *msg)
            182{
            183        FILE *fp;
            184        fp = fopen("/tmp/apache_mod.log","a+");
            185        fputs(msg,fp);
            186        fputs("|n",fp);
            187        fclose(fp);
            188}

            189
            190
            191/**
            192
            193*
            194* @param username 用戶名
            195*                t        取類型   目前用到了二種 gif txt
            196*
            197* @return 返回文件的物理path
            198*
            199*/

            200
            201static char *get_file_path(char *username, char *type)
            202{
            203        char *filepath;
            204        int i,ii,m=100;
            205
            206        i = toascii(username[0]);
            207        ii = toascii(username[1]);
            208        m = strlen(ROOT_PATH)+18+strlen(username);
            209
            210        filepath = (char *) malloc(m);
            211        bzero(filepath,m);
            212
            213        sprintf(filepath,"%s/%c/%d/%s.%s",ROOT_PATH,username[0],(i+ii)%MODS,username,type);
            214
            215        return (char *)filepath;
            216}

            217
            218//返回web path
            219static char *get_web_path(char *username)
            220{
            221        char *filepath;
            222        int i,ii,m=100;
            223
            224        i = toascii(username[0]);
            225        ii = toascii(username[1]);
            226        m = strlen(DOMAIN)+18+strlen(username);
            227
            228        filepath = (char *) malloc(m);
            229
            230        sprintf(filepath,"%s/%c/%d/%s.gif",DOMAIN, username[0], (i+ii)%MODS, username);
            231
            232        return (char *)filepath;
            233
            234}

            235
            236static const handler_rec useravatar_handlers[] =
            237{
            238    "get_useravatar", useravatar_handler },
            239    { NULL }
            240}
            ;
            241
            242
            243module useravatar_module =
            244{
            245    STANDARD_MODULE_STUFF,
            246    NULL,                       /* module initializer */
            247    NULL,                       /* per-directory config creator */
            248    NULL,                       /* dir config merger */
            249    NULL,                       /* server config creator */
            250    NULL,                       /* server config merger */
            251    NULL,                       /* command table */
            252    useravatar_handlers,        /* [7] list of handlers */
            253    NULL,                       /* [2] filename-to-URI translation */
            254    NULL,                       /* [5] check/validate user_id */
            255    NULL,                       /* [6] check user_id is valid *here* */
            256    NULL,                       /* [4] check access by host address */
            257    NULL,                       /* [7] MIME type checker/setter */
            258    NULL,                       /* [8] fixups */
            259    NULL,                       /* [10] logger */
            260    NULL,                       /* [3] header parser */
            261    NULL,                       /* process initializer */
            262    NULL,                       /* process exit/cleanup */
            263    NULL                        /* [1] post read_request handling */
            264}
            ;

            posted on 2008-01-30 16:35 編程之道 閱讀(2008) 評論(0)  編輯 收藏 引用 所屬分類: C/C++web編程開發相關

            精品国产青草久久久久福利| 婷婷久久综合| 久久精品免费一区二区| 精品无码久久久久久久动漫| 国产午夜精品理论片久久影视| 日韩久久久久久中文人妻| 欧美一区二区久久精品| 午夜精品久久久久久久无码| 久久久久国产一区二区三区| 精品国产婷婷久久久| 中文精品久久久久国产网址| 久久99精品国产麻豆不卡| 99久久国产热无码精品免费久久久久 | 久久人妻少妇嫩草AV无码专区| 久久精品国产AV一区二区三区 | 久久国产色AV免费看| 久久se精品一区二区| 99热精品久久只有精品| 久久久久久亚洲精品不卡| 久久青青草视频| 亚洲午夜久久久影院| av午夜福利一片免费看久久| 国产AⅤ精品一区二区三区久久| 狠狠色伊人久久精品综合网| 久久国语露脸国产精品电影| 国内精品久久久久久99蜜桃| 国产精品亚洲美女久久久| 久久久无码精品午夜| 色欲综合久久中文字幕网| 国产成人精品久久一区二区三区av| 少妇久久久久久被弄到高潮| 午夜久久久久久禁播电影| 久久久中文字幕| 久久人人爽人人爽人人爽| 久久精品国产亚洲一区二区| 伊人久久国产免费观看视频| 91久久精一区二区三区大全| 色99久久久久高潮综合影院| 精品亚洲综合久久中文字幕| 久久天天躁狠狠躁夜夜2020一 | 国产亚洲精久久久久久无码|