linux apache模块开发

6 篇文章 0 订阅

 /***************************************************************************

 *   Copyright (C) 2009 by root                                            *
 *   root@localhost.localdomain                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <apr_hash.h>
#include <ap_config.h>
#include <apr_md5.h>
#include <time.h>
//#include <iconv.h>
//#include <md5global.h>
//#include <md5.h>
#include "B21Ipcdef.h"
#include "B21Define.h"
#include "B21Inv.h"
#include "B21Real.h"
#include "B21Invdef.h"
#include "platform/B21Termtbl.h"
#define MAX_SIZE 1024
char* PGNAME;
 module AP_MODULE_DECLARE_DATA publish_module;
apr_hash_t *parse_args_extra(request_rec *r,char *args)
{
apr_array_header_t *values;
apr_hash_t *form;
char *pair;
char *eq;
const char *delim = "&";
char *last;
char **ptr;
if(args == NULL)
return NULL;
form = apr_hash_make(r->pool);
pair = apr_strtok(args,delim,&last);
//while(pair != NULL)
//{
eq = strchr(pair, '=');
// if(eq == NULL)
// {
// pair = apr_strtok(NULL, delim, &last);
// continue;
// }
*eq++ = '/0';
ap_unescape_url(pair);
ap_unescape_url(eq+1);
values = apr_hash_get(form, pair, APR_HASH_KEY_STRING);
if(values == NULL)
{
values = apr_array_make(r->pool, 1, sizeof(const char*));
apr_hash_set(form, pair, APR_HASH_KEY_STRING, values);
}
ptr = apr_array_push(values);
*ptr = apr_pstrdup(r->pool, eq+1);
// pair = apr_strtok(NULL, delim, &last);
//}
return form;
}
static int parse_form_from_POST(request_rec *r)
{
  int bytes, eos;
  apr_size_t count;
  apr_status_t rv;
  apr_bucket_brigade *bb;
  apr_bucket_brigade *bbin;
  char *buf;
  apr_bucket *b;
  const char *clen=apr_table_get(r->headers_in,"Content-Length");
  if (clen != NULL)
    {
     bytes=strtol(clen,NULL,0);
     if (bytes>=MAX_SIZE)
        {
         return HTTP_REQUEST_ENTITY_TOO_LARGE;
        }
    }
  else
  { bytes=MAX_SIZE;}
bb=apr_brigade_create(r->pool,r->connection->bucket_alloc);
bbin=apr_brigade_create(r->pool,r->connection->bucket_alloc);
count=0;
eos=0;
do
 {
  rv=ap_get_brigade(r->input_filters,bbin,AP_MODE_READBYTES,APR_BLOCK_READ,bytes);
  if (rv !=APR_SUCCESS)
    {return HTTP_INTERNAL_SERVER_ERROR; } 
 for (b=APR_BRIGADE_FIRST(bbin);  b!=APR_BRIGADE_SENTINEL(bbin); b=APR_BUCKET_NEXT(b)   )
  {
  if ((APR_BUCKET_IS_EOS(b)) || (b->length>MAX_SIZE))
    { eos=1;
      break;
    }
  if (! APR_BUCKET_IS_METADATA(b))
    {
     if (b->length!=(apr_size_t)(-1)) 
       {
        count+= b->length;
        //if (count>MAX_SIZE)
        //  {apr_bucket_delete(b);}
       } 
    } 
   // if (count<=MAX_SIZE)
     //{ 
      APR_BUCKET_REMOVE(b);
      APR_BRIGADE_INSERT_TAIL(bb,b);
    // }
   }//for
 } while(!eos);//do
 if (count>MAX_SIZE)
   {return HTTP_REQUEST_ENTITY_TOO_LARGE;}
  buf=apr_palloc(r->pool,count+1);
  rv=apr_brigade_flatten(bb,buf,&count);
  if (rv !=APR_SUCCESS) 
   {return HTTP_INTERNAL_SERVER_ERROR;}
  buf[count]='/0';
  //ap_rputs(buf,r);
  apr_hash_t *form=NULL;
  form=parse_args_extra(r,buf);
  apr_array_header_t *arr = apr_hash_get(form, "message", APR_HASH_KEY_STRING);
if(arr == NULL)
return HTTP_BAD_REQUEST;
char **ptr = apr_array_pop(arr);
        //ap_rputs(*ptr,r);
  
  char *part1;
   char *part2;
  char division1[]="}{";
  char division2[]=","; 
  
 // char *s;
 // int ch='=';
  // s=strchr(*buf,ch);
   //s++;
   char *pdest;
   int i;
   INVE103 pac;
   char typein[2];
   char type[2];
   char date[9];
   char time[7];
   int t1,t2;
      t1=t2=0;
   part1=strtok(*ptr,division1);
   while (part1!=NULL)
   {
        i=1;
       part2=strtok(part1,division2);
       while(part2!=NULL)
       {
         switch (i){
case 1://time 
 memcpy(date,part2,8);              
          memcpy(time,part2+8,6); 
 strcpy(pac.data,part2);  
 strcat(pac.data,",");           
 break;
case 2:// type
  strcpy(type,part2);
 //iconv_t a= iconv_open("gb2312","utf-8");
 //if (a==0) 
 //{
 //  ap_rputs("501",r);  
 //  return OK;
 //}
  //t1=sizeof(typein);
  //t2=sizeof(type);
  //if (iconv(a,&typein,&t1,&type,&t2)==-1)
  //{
  // ap_rputs("501",r);  
  // return OK;
  //}
  //iconv_close(a);
  strcat(pac.data,part2);  
  strcat(pac.data,",");          
          break;  
case 3: // code
 strcat(pac.data,part2);
 strcat(pac.data,","); 
 //memcpy(pac.data[1],part,sizeof(part));  
          break; 
 case 4: //content
 // if(memcmp(type,"1",sizeof("1"))==0) 
 //  strcat(pac.data,","); 
  strcat(pac.data,part2);
  strcat(pac.data,","); 
 break;                                      
case 5://attribute
 // if(memcmp(type,"1",sizeof("1"))==0) 
 // strcat(pac.data,",");  
  strcat(pac.data,part2);
  strcat(pac.data,","); 
 break;
case 6://associate attribute
 pdest=strchr(part2, '}');
 if (pdest!=NULL)
    *pdest='/0';
 if(memcmp(type,"1",sizeof("1"))==0)  
  strcat(pac.data,part2);
 if(memcmp(type,"2",sizeof("2"))==0) 
 {
  char temp[100];
  strcpy(temp,pac.data); 
  snprintf(pac.data,MAX_MSG_LEN-8,"%s%s",temp,part2); 
  strcat(pac.data,",");   
 }
 break;  
case 7:
strcat(pac.data,part2);
strcat(pac.data,","); 
break;
case 8:
pdest=strchr(part2, '}');
if (pdest!=NULL)
   *pdest='/0';
strcat(pac.data,part2);
break; 
default:
 ap_rputs("504",r);  
 return OK;
}
i++;
         part2=strtok(NULL,division2);
       }
      if(memcmp(type,"1",sizeof("1"))==0) 
      {
        memcpy(pac.type,"08",sizeof("08"));
      }
      else
      {
     memcpy(pac.type,"09",sizeof("09"));
      }
     memcpy(pac.date,date,8);              
     memcpy(pac.time,time,6);  
    sprintf(pac.size ,"%d",INVE103_SIZE-256+strlen(pac.data));
    Put_Inve103MM(&pac);
    part1=strtok(NULL,division1);
    }
    ap_rputs("200",r);   
   return OK;
}
typedef struct publish_config{
char *passip;
} publish_config;
static void *create_publish_config(apr_pool_t *p,server_rec *s)
{
publish_config *newcfg;
newcfg=(publish_config*)apr_pcalloc(p,sizeof(publish_config));
return (void*)newcfg;
}
static const char *set_publish_string(cmd_parms *parms,void *mconfig,const char *arg)
{
publish_config *s_cfg=ap_get_module_config(parms->server->module_config,&publish_module);
if (!strcmp(parms->cmd->name,"passip")){
s_cfg->passip=(char*)arg;
}
return NULL;
}
static const command_rec mod_publish_cmds[]={
 AP_INIT_TAKE1(
  "passip",
  set_publish_string,
  NULL,
  RSRC_CONF,
  NULL
  ),
 {NULL}
};
void write_file()
{
 int fd;
 time_t clock;
 char buf[30];
 struct tm *tm;
 time(&clock);
 tm=gmtime(&clock);
 strftime(buf,sizeof(buf),"%Y%m%d%H%M%S",tm);
 //fd=fopen("count.txt","a+");
 fd=open("/1.txt",O_APPEND,S_IRWXU);
 if(fd)
 { 
   //fputs("||",fd);
   write(fd,"II",strlen("II"));
   write(fd,buf,strlen(buf));
   //fputs(buf,fd);
   close(fd);
 }
}
static int publish_handler(request_rec *r)
{
   char **ptr;
   int rv=OK;
   apr_hash_t *form=NULL;
   apr_array_header_t *g_array;
     if(!r->handler || strcmp(r->handler,"publish")){
return DECLINED;
}
//  write_file();
const char *pass=apr_table_get(r->headers_in,"News-Pass");
if(pass)
{
 
   
 char *output1;  
 //char ip[60]="61.172.197.73JSY";
 char ip[60];
 publish_config *s;
          s=ap_get_module_config(r->server->module_config,&publish_module);
 //strcat(ip,s->passip);
 memset(ip,0x00,sizeof(ip));
 //memcpy(ip,s->passip,sizeof(s->passip));
 strcpy(ip,s->passip);
 time_t clock;
 char buf[30];
 struct tm *tm;
 time(&clock);
 tm=gmtime(&clock);
 strftime(buf,sizeof(buf),"%Y%m%d",tm);
 strcat(ip,buf);
 output1=ap_md5(r->pool,ip);
// unsigned int len=strlen(ip);
// int i;
// MD5Init(&context);
// MD5Update(&context,(unsigned char*)ip,len); 
// MD5Final(digest,&context); 
// sprintf(output1,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",digest[0]//,digest[1],digest[2],digest[3],digest[4],digest[5],digest[6],digest[7],digest[8],digest[9],d//igest[10],digest[11],digest[12],digest[13],digest[14],digest[15]);
// for(i=0;i<16;i++)
 //{
 // sprintf(&(output1[2*i]),"%02X",(unsigned char)digest);
  //sprintf(&(output1[2*i+1]),"%02X",(unsigned char)(digest));
 // digest=digest+4;
 //}  
 
 if (strcmp(pass,output1)==0) 
 {
   //ap_rputs(output1,r);
   //ap_rputs("Pass",r);
 }
 else
 {    
   ap_rputs("No Pass",r);
   return OK;
 }    
        }
else
{
 ap_rputs("invoid value!",r);
 return OK;
}
      
     if (r->method_number==M_GET)
      {
       //form=parse_args(r,r->args);       
     
      }
   if (r->method_number==M_POST)
     {
      rv=parse_form_from_POST(r);
      if (rv !=OK)
      {ap_rputs("<p>error reading  form data found<p>",r);}
    //  write_file();
      return OK; 
     }
   
  
}
static void publish_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(publish_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA publish_module = {
    STANDARD20_MODULE_STUFF, 
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    create_publish_config,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    mod_publish_cmds,                  /* table of config file commands       */
    publish_register_hooks  /* register hooks                      */
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值