File: epapfilter.c

package info (click to toggle)
libembperl-perl 2.5.0-10%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,128 kB
  • sloc: ansic: 21,384; perl: 14,496; javascript: 4,280; cpp: 467; xml: 49; makefile: 33; sh: 24
file content (406 lines) | stat: -rw-r--r-- 14,876 bytes parent folder | download | duplicates (6)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*###################################################################################
#
#   Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh  www.ecos.de
#   Embperl - Copyright (c) 2008-2014 Gerald Richter
#
#   You may distribute under the terms of either the GNU General Public
#   License or the Artistic License, as specified in the Perl README file.
#   For use with Apache httpd and mod_perl, see also Apache copyright.
#
#   THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#   WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#   $Id: epapfilter.c 1578075 2014-03-16 14:01:14Z richter $
#
###################################################################################*/


#include "ep.h"

#ifdef APACHE2

#include <util_filter.h>
#include <http_request.h>

/* ------------------------------------------------------------------------ */
/*                                                                          */
/*!         Provider that acts as Apache output filter                      */
/*                                                                          */

typedef struct tProviderApOutFilter
    {
    tProvider           Provider ;
    const char *        sURI ;         /**< Filename */
    } tProviderApOutFilter ;


/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ProviderApOutFilter_New    					            */
/*                                                                          */
/*! 
*   \_en
*   Creates a new Apache Output Filter provider and fills it with data from the hash pParam
*   The resulting provider is put into the cache structure
*   
*   @param  r               Embperl request record
*   @param  pItem           CacheItem which holds the output of the provider
*   @param  pProviderClass  Provider class record
*   @param  pProviderParam  Parameter Hash of this Providers
*                               subreq          URI
*   @param  pParam          All Parameters 
*   @param  nParamIndex       If pParam is an AV, this parameter gives the index into the Array
*   @return                 error code
*   \endif                                                                       
*
*   \_de									   
*   Erzeugt einen neuen Apache Output Filter Provider. Der Zeiger
*   auf den resultierenden Provider wird in die Cache Struktur eingefgt
*   
*   @param  r               Embperl request record
*   @param  pItem           CacheItem welches die Ausgabe des Providers 
*                           speichert
*   @param  pProviderClass  Provider class record
*   @param  pProviderParam  Parameter Hash dieses Providers
*                               subreq          URI
*   @param  pParam          Parameter insgesamt
*   @param  nParamIndex       Wenn pParam ein AV ist, gibt dieser Parameter den Index an
*   @return                 Fehlercode
*   \endif                                                                       
*                                                                          
* ------------------------------------------------------------------------ */

static int ProviderApOutFilter_New (/*in*/ req *              r,
                             /*in*/ tCacheItem *       pItem,
                             /*in*/ tProviderClass *   pProviderClass,
                             /*in*/ HV *               pProviderParam,
                             /*in*/ SV *               pParam,
                             /*in*/ IV                 nParamIndex)


    {
    epTHX_
    int          rc ;
    tProviderApOutFilter * pNew  ;
    char *          sURI ;
    
    if ((rc = Provider_New (r, sizeof(tProviderApOutFilter), pItem, pProviderClass, pProviderParam)) != ok)
        return rc ;

    pNew = (tProviderApOutFilter *)pItem -> pProvider ;

    sURI = GetHashValueStr (aTHX_ pProviderParam, "subreq",  r -> Component.Param.sSubreq) ;
    pNew -> sURI = sURI ;
    if (!pNew -> sURI)
        {
        strncpy (r -> errdat1, sURI, sizeof (r -> errdat1) - 1) ;
        return rcNotFound ;
        }

    return ok ;
    }


/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ProviderApOutFilter_AppendKey    				            */
/*                                                                          */
/*! 
*   \_en
*   Append it's key to the keystring. If it depends on anything it must 
*   call Cache_AppendKey for any dependency.
*   The Apache Output Filter provider appends the URI
*   
*   @param  r               Embperl request record
*   @param  pProviderClass  Provider class record
*   @param  pProviderParam  Parameter Hash of this Providers
*                               subreq          URI
*   @param  pParam          All Parameters 
*   @param  nParamIndex       If pParam is an AV, this parameter gives the index into the Array
*   @param  pKey            Key to which string should be appended
*   @return                 error code
*   \endif                                                                       
*
*   \_de									   
*   Hngt ein eigenen Schlssel an den Schlsselstring an. Wenn irgednwelche
*   Abhnigkeiten bestehen, mu Cache_AppendKey fr alle Abhnigkeiten aufgerufen 
*   werden.
*   Der Apache Output Filter hngt die URI an.
*   
*   @param  r               Embperl request record
*   @param  pProviderClass  Provider class record
*   @param  pProviderParam  Parameter Hash dieses Providers
*                               subreq          URI
*   @param  pParam          Parameter insgesamt
*   @param  nParamIndex       Wenn pParam ein AV ist, gibt dieser Parameter den Index an
*   @param  pKey            Schlssel zu welchem hinzugefgt wird
*   @return                 Fehlercode
*   \endif                                                                       
*                                                                          
* ------------------------------------------------------------------------ */

static int ProviderApOutFilter_AppendKey (/*in*/ req *              r,
                                   /*in*/ tProviderClass *   pProviderClass,
                                   /*in*/ HV *               pProviderParam,
                                   /*in*/ SV *               pParam,
                                   /*in*/ IV                 nParamIndex,
                                   /*i/o*/ SV *              pKey)
    {
    epTHX_
    const char * sURI  ;

    sURI = GetHashValueStr (aTHX_ pProviderParam, "subreq",  r -> Component.Param.sSubreq) ;
    if (!sURI)
        {
        strncpy (r -> errdat1, sURI, sizeof (r -> errdat1) - 1) ;
        return rcNotFound ;
        }
    
    sv_catpvf (pKey, "*subreq:%s", sURI) ;
    return ok ;
    }

/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ProviderApOutFilter_Callback    				            */
/*                                                                          */
/*! 
*   \_en
*   This callback is call from Apache when any output is available.
*   It gather the output in one SV.
*   
*   @param  f               Apache Filter Record
*   @param  bb              Apache Bucket Brigade
*   @return                 error code
*   \endif                                                                       
*
*   \_de									   
*   Dieses Callback wird von Apache aufgerufen, wenn Daten zur verfgung stehen.
*   Alle Daten werden in einem SV gesammelt.
*   
*   @param  f               Apache Filter Record
*   @param  bb              Apache Bucket Brigade
*   @return                 error code
*   \endif                                                                       
*                                                                          
* ------------------------------------------------------------------------ */

struct tProviderApOutFilter_CallbackData
    {
    tReq *      pReq ;
    SV *        pData ;
    } ;


static apr_status_t ProviderApOutFilter_Callback(ap_filter_t *f, apr_bucket_brigade *bb)
    {
    /*request_rec *ap_r = f->r;*/
    struct tProviderApOutFilter_CallbackData * ctx = (struct tProviderApOutFilter_CallbackData *)(f->ctx);
    tReq * r = ctx -> pReq ;
    apr_bucket *b;
    apr_size_t len;
    const char *data;
    apr_status_t rv;
    char buf[4096];
    epTHX_


    //APR_BRIGADE_FOREACH(b, bb) 
    for (b = APR_BRIGADE_FIRST(bb);
         b != APR_BRIGADE_SENTINEL(bb);
         b = APR_BUCKET_NEXT(b)) 
        {
        /* APR_BUCKET_IS_EOS(b) does give undefined symbol, when running outside of Apache */
        /* if (APR_BUCKET_IS_EOS(b)) */
        if (strcmp (b -> type -> name, "EOS") == 0)
            {
            break;
            }

        rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
        if (rv != APR_SUCCESS) 
            {
            sprintf (buf, "%d", rv) ;
            LogErrorParam (r -> pApp, rcApacheErr, buf, "apr_bucket_read()");
            return rv;
            }

        if (len > 0)
            {
            if (!ctx -> pData)
                ctx -> pData = newSV(len) ;
            sv_catpvn (ctx -> pData, data, len) ;
            }
        }

    apr_brigade_destroy(bb);

    return APR_SUCCESS;
    }


/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ProviderApOutFilter_GetContentSV  				            */
/*                                                                          */
/*! 
*   \_en
*   Get the whole content from the provider. 
*   The Apache Output Filter provider starts a subreqest and reads the 
*   whole result into memory
*   
*   @param  r               Embperl request record
*   @param  pProvider       The provider record
*   @param  pData           Returns the content
*   @param  bUseCache       Set if the content should not recomputed
*   @return                 error code
*   \endif                                                                       
*
*   \_de									   
*   Holt den gesamt Inhalt vom Provider.
*   Der Apache Output Filter Provider started einen Sub-Request und
*   liet das komplette Ergebnis in den Speicher
*   
*   
*   @param  r               Embperl request record
*   @param  pProvider       The provider record
*   @param  pData           Liefert den Inhalt
*   @param  bUseCache       Gesetzt wenn der Inhalt nicht neu berechnet werden soll
*   @return                 Fehlercode
*   \endif                                                                       
*                                                                          
* ------------------------------------------------------------------------ */



static int ProviderApOutFilter_GetContentSV (/*in*/ req *             r,
                             /*in*/ tProvider *     pProvider,
                             /*in*/ SV * *              pData,
                             /*in*/ bool                bUseCache)

    {
    epTHX_
    int rc = ok ;
    char * sURI ;
    request_rec *rr = NULL;
    struct tProviderApOutFilter_CallbackData ctx ;
    ap_filter_rec_t frec ;
    ap_filter_t filter ;

    ctx.pReq    = r ;
    ctx.pData   = NULL ;
    
    memset (&frec, 0, sizeof(frec)) ;
    frec.name = "Embperl_ProviderApOutFilter" ;
    frec.filter_func.out_func = &ProviderApOutFilter_Callback ;
    frec.next        = NULL ;
    frec.ftype       = AP_FTYPE_RESOURCE ;
        
    filter.frec = &frec ;
    filter.ctx  = &ctx ;
    filter.next = NULL ;
    filter.r    = r -> pApacheReq ;
    filter.c    = filter.r -> connection ;


    sURI = r -> Component.sSourcefile = (char *)((tProviderApOutFilter *)pProvider) -> sURI ;

    if (!bUseCache)
        {
        if (strncmp(sURI, "http://", 7) == 0 || strncmp(sURI, "ftp://", 7) == 0)
            rr = ap_sub_req_lookup_file(apr_pstrcat (r -> pApacheReq -> pool, "proxy:", sURI, NULL), r -> pApacheReq, &filter);
        else
            rr = ap_sub_req_lookup_uri(sURI, r -> pApacheReq, &filter);

        if (!rr || rr->status != HTTP_OK) 
            {
            rc = rr->status ;
            strncpy (r -> errdat1, r -> Component.sSourcefile, sizeof (r -> errdat1)) ;
            return rr -> status ;
            }

        rc = ap_run_sub_req(rr) ;

        if (rc || rr->status != HTTP_OK) 
            {
            if (rc == 0)
                {
                strncpy (r -> errdat1, r -> Component.sSourcefile, sizeof (r -> errdat1)) ;
                return rr -> status ;
                }
            else
                {
                sprintf (r -> errdat1, "%d (status=%d)", rc, rr -> status) ;
                strncpy (r -> errdat2, r -> Component.sSourcefile, sizeof (r -> errdat2)) ;
                return rc;
                }
            }
            
        if (rr != NULL) 
            ap_destroy_sub_req(rr);

   
        if (rc == ok)
            {
            /* SvREFCNT_inc (ctx.pData) ; */
            if (ctx.pData)
                {
                r -> Component.pBuf = SvPVX (ctx.pData) ;
                r -> Component.pEndPos = r -> Component.pBuf + SvLEN(ctx.pData) ;
                r -> Component.pCurrPos = r -> Component.pBuf ;
                }
            *pData = ctx.pData ;
            }
        }

    return rc ;
    }


/* ------------------------------------------------------------------------ */


tProviderClass ProviderClassApOutFilter = 
    {   
    "text/*", 
    &ProviderApOutFilter_New, 
    &ProviderApOutFilter_AppendKey, 
    NULL,
    &ProviderApOutFilter_GetContentSV,
    NULL,
    NULL,
    NULL,
    } ;

/* ------------------------------------------------------------------------ */
/*                                                                          */
/* ApFilter_Init      					                    */
/*                                                                          */
/*! 
*   \_en
*   Register all the providers
*   @return                 error code
*   
*   \endif                                                                       
*
*   \_de									   
*   Provider registrieren
*   @return                 Fehlercode
*   
*   \endif                                                                       
*                                                                          
* ------------------------------------------------------------------------ */

int ApFilter_Init (/*in*/ tApp * a)

    {
    Cache_AddProviderClass ("apoutfilter",      &ProviderClassApOutFilter) ;

    return ok ;
    }



#endif /* APACHE2 */