00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <unistd.h>
00034 #include <iostream>
00035
00036 using std::cerr ;
00037 using std::endl ;
00038
00039 #include "httpd.h"
00040 #include "http_config.h"
00041 #include "http_core.h"
00042 #include "http_log.h"
00043 #include "http_protocol.h"
00044 #include "http_request.h"
00045 #include "http_main.h"
00046 #include "util_script.h"
00047 #include "util_md5.h"
00048
00049 #include "BESDataRequestInterface.h"
00050 #include "BESApacheWrapper.h"
00051
00052 char * ltoa(long val, char *buf,int base)
00053 {
00054 ldiv_t r;
00055 if (base > 36 || base < 2)
00056 {
00057 *buf = '\0';
00058 return buf;
00059 }
00060 if (val < 0)
00061 *buf++ = '-';
00062 r = ldiv (labs(val), base);
00063
00064 if (r.quot > 0)
00065 buf = ltoa ( r.quot, buf, base);
00066
00067
00068
00069 *buf++ = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem];
00070 *buf = '\0';
00071 return buf;
00072 }
00073
00074
00075 static int util_read(request_rec *r, const char **rbuf)
00076 {
00077 int rc = OK;
00078
00079 if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
00080 {
00081 return rc;
00082 }
00083
00084 if (ap_should_client_block(r))
00085 {
00086 char argsbuffer[HUGE_STRING_LEN];
00087 int rsize, len_read, rpos=0;
00088 long length = r->remaining;
00089
00090 *rbuf = (char*) apr_pcalloc(r->pool, length + 1);
00091
00092 ap_hard_timeout("util_read", r);
00093
00094 while((len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0)
00095 {
00096 ap_reset_timeout(r);
00097 if ((rpos + len_read) > length)
00098 {
00099 rsize = length - rpos;
00100 }
00101 else
00102 {
00103 rsize = len_read;
00104 }
00105 memcpy((char*)*rbuf + rpos, argsbuffer, rsize);
00106 rpos += rsize;
00107 }
00108
00109 ap_kill_timeout(r);
00110 }
00111 return rc;
00112 }
00113
00114 static int
00115 header_trace( void *data, const char *key, const char *val )
00116 {
00117 request_rec *r = (request_rec *)data ;
00118 cerr << "Header Field '" << key << "' = '" << val << "'" << endl ;
00119 return TRUE ;
00120 }
00121
00122 static void
00123 list_headers( request_rec *r )
00124 {
00125
00126 apr_table_do( header_trace, r, r->headers_in, NULL ) ;
00127 }
00128
00129 static int opendap_handler(request_rec *r)
00130 {
00131 char port_number_buffer[80];
00132 dup2(r->connection->client->fd,STDOUT_FILENO);
00133 BESDataRequestInterface rq;
00134
00135
00136 rq.server_name=0;
00137 rq.server_address=0;
00138 rq.server_protocol=0;
00139 rq.server_port=0;
00140 rq.script_name=0;
00141 rq.user_address=0;
00142 rq.user_agent=0;
00143 rq.request=0;
00144
00145
00146 rq.server_name=ap_get_server_name(r);
00147 rq.server_address="jose";
00148 rq.server_protocol=r->protocol;
00149 ltoa(ap_get_server_port(r), port_number_buffer, 10);
00150 rq.server_port=port_number_buffer;
00151 rq.script_name=r->uri;
00152 rq.user_address=r->connection->remote_ip;
00153
00154 rq.user_agent = apr_table_get(r->headers_in, "User-Agent");
00155
00156 const char* m_method = r->method;
00157 if (!m_method)
00158 {
00159 cerr << "mod_opendap: Fatal, Can not load request method" << endl;
00160
00161 return HTTP_INTERNAL_SERVER_ERROR;
00162 }
00163
00164 BESApacheWrapper wrapper;
00165 if ( strcmp(m_method, "GET") == 0 )
00166 {
00167 if(r->parsed_uri.query)
00168 {
00169 wrapper.process_request(r->parsed_uri.query);
00170 rq.cookie=wrapper.process_user(r->parsed_uri.query);
00171 rq.token=wrapper.process_token(r->parsed_uri.query);
00172 }
00173 else
00174 {
00175 rq.request=0;
00176 rq.cookie=0;
00177 rq.token=0;
00178 }
00179 }
00180 else if (strcmp(m_method, "POST") == 0 )
00181 {
00182 const char *post_data=0;
00183 util_read(r, &post_data);
00184 wrapper.process_request(post_data);
00185 rq.cookie=wrapper.process_user(post_data);
00186 rq.token=wrapper.process_token(r->parsed_uri.query);
00187 }
00188 else
00189 {
00190 rq.request=0;
00191 rq.cookie=0;
00192 rq.token=0;
00193 }
00194
00195
00196
00197
00198
00199
00200 if( !rq.cookie || !strcmp(rq.cookie,"") )
00201 {
00202
00203 rq.cookie = apr_table_get( r->headers_in, "Cookie" ) ;
00204 }
00205
00206 int status = 0 ;
00207 rq.request = wrapper.get_first_request() ;
00208 while( rq.request && status == 0 )
00209 {
00210 status = wrapper.call_BES(rq);
00211 rq.request = wrapper.get_next_request() ;
00212 }
00213
00214
00215
00216 cout << flush ;
00217
00218
00219 exit( 0 ) ;
00220
00221 return OK;
00222 }
00223
00224
00225 static handler_rec opendap_handlers[] =
00226 {
00227 {"opendap-handler", opendap_handler},
00228 {NULL}
00229 };
00230
00231
00232 module MODULE_VAR_EXPORT opendap_module =
00233 {
00234 STANDARD_MODULE_STUFF,
00235 NULL,
00236 NULL,
00237 NULL,
00238 NULL,
00239 NULL,
00240 NULL,
00241 opendap_handlers,
00242 NULL,
00243 NULL,
00244 NULL,
00245 NULL,
00246 NULL,
00247 NULL,
00248 NULL,
00249 NULL,
00250 NULL,
00251 NULL,
00252 NULL
00253 };
00254