Apache模块 开发实例(转)
1
/**/
/**
2* Copyright 2003 Tom, Inc. All rights reserved.
3*
4* Description: Apache模块 取用户图片
5* FileName: useravatar.c
6*
7* @Author sam
8* <[email protected]>
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" 为 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图片地址
44 const char * DEFAULT_AVATAR = " /www/web/public/default.gif " ;
45 const char * ROOT_PATH = " /www/web/public " ;
46 const 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
50 const char * DOMAIN = " http://avatartest.tom.com/public " ;
51 const char * DEFAULT_WEB_AVATAR = " http://avatartest.tom.com/public/default.gif " ;
52
53 module MODULE_VAR_EXPORT useravatar_module;
54
55 static void redirect(request_rec * r, const char * path);
56 static char * open_avatars_txt(request_rec * r, char * username);
57 static char * get_file_path( char * username, char * type);
58 static char * get_web_path( char * username);
59 static void log( char * msg);
60 static int checkfile( char * path);
61
62
63 static 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
112 static 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*/
128 static 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*/
147 static 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
181 static 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
201 static 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
219 static 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
236 static const handler_rec useravatar_handlers[] =
237 {
238 { "get_useravatar", useravatar_handler },
239 { NULL }
240} ;
241
242
243 module 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} ;
2* Copyright 2003 Tom, Inc. All rights reserved.
3*
4* Description: Apache模块 取用户图片
5* FileName: useravatar.c
6*
7* @Author sam
8* <[email protected]>
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" 为 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图片地址
44 const char * DEFAULT_AVATAR = " /www/web/public/default.gif " ;
45 const char * ROOT_PATH = " /www/web/public " ;
46 const 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
50 const char * DOMAIN = " http://avatartest.tom.com/public " ;
51 const char * DEFAULT_WEB_AVATAR = " http://avatartest.tom.com/public/default.gif " ;
52
53 module MODULE_VAR_EXPORT useravatar_module;
54
55 static void redirect(request_rec * r, const char * path);
56 static char * open_avatars_txt(request_rec * r, char * username);
57 static char * get_file_path( char * username, char * type);
58 static char * get_web_path( char * username);
59 static void log( char * msg);
60 static int checkfile( char * path);
61
62
63 static 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
112 static 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*/
128 static 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*/
147 static 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
181 static 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
201 static 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
219 static 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
236 static const handler_rec useravatar_handlers[] =
237 {
238 { "get_useravatar", useravatar_handler },
239 { NULL }
240} ;
241
242
243 module 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} ;