File: execget.c

package info (click to toggle)
mnogosearch 3.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 17,484 kB
  • ctags: 4,565
  • sloc: ansic: 94,097; xml: 16,864; sh: 8,915; makefile: 1,727; perl: 801; php: 561; sql: 15
file content (111 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (C) 2000-2006 Lavtech.com corp. All rights reserved.

   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 
*/

#include "udm_config.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#endif

#include "udm_common.h"
#include "udm_utils.h"
#include "udm_execget.h"
#include "udm_log.h"


/*
#define DEBUG_EXECGET
*/

int UdmExecGet(UDM_AGENT * Indexer,UDM_DOCUMENT * Doc){
	int res;
	char * args;
	char cmdline[1024];
	FILE * f;
	UDM_URL *Url=&Doc->CurURL;

	Doc->Buf.buf[Doc->Buf.size = 0] = '\0';

	if((args = strchr(UDM_NULL2EMPTY(Url->filename), '?'))) {
		*args='\0';
		args++;
	}
	sprintf(cmdline, "%s%s", UDM_NULL2EMPTY(Url->path), UDM_NULL2EMPTY(Url->filename));
	
	if(!strcmp(UDM_NULL2EMPTY(Url->schema), "exec")) {
		if(args){
			sprintf(UDM_STREND(cmdline)," \"%s\"",args);
		}
	}else
	if(!strcmp(UDM_NULL2EMPTY(Url->schema), "cgi")) {
		/* Non-parsed-headers CGI return HTTP status itself */
		if(strncasecmp(UDM_NULL2EMPTY(Url->filename), "nph-", 4)) {
			sprintf(Doc->Buf.buf,"HTTP/1.0 200 OK\r\n");
			Doc->Buf.size = strlen(Doc->Buf.buf);
		}
		UdmSetEnv("QUERY_STRING",args?args:"");
		UdmSetEnv("REQUEST_METHOD","GET");
	}

#ifdef DEBUG_EXECGET
	fprintf(stderr,"cmd='%s'\n",cmdline);
#endif

	UdmLog(Indexer,UDM_LOG_DEBUG,"Starting program '%s'",cmdline);

	f=popen(cmdline,"r");

	/* Remove set variables */
	if(!strcmp(UDM_NULL2EMPTY(Url->schema), "cgi")) {
		UdmUnsetEnv("REQUEST_METHOD");
		UdmUnsetEnv("QUERY_STRING");
	}

	if(f){
		int fd,bytes;
		
		fd=fileno(f);
		while((bytes = read(fd, Doc->Buf.buf + Doc->Buf.size, Doc->Buf.maxsize - Doc->Buf.size))){
			Doc->Buf.size += bytes;
			Doc->Buf.buf[Doc->Buf.size] = '\0';
		}
		res=pclose(f);
	}else{
		int status;
		printf("error=%s\n",strerror(errno));
		switch(errno){
			case ENOENT: status=404;break; /* Not found */
			case EACCES: status=403;break; /* Forbidden*/
			default: status=500;
		}
		sprintf(Doc->Buf.buf,"HTTP/1.0 %d %s\r\n\r\n",status,strerror(errno));
		Doc->Buf.size = strlen(Doc->Buf.buf);
	}
#ifdef DEBUG_EXECGET
	fprintf(stderr,"buf='%s'\n",Indexer->buf);
#endif
	return(Doc->Buf.size);
}