DBA Data[Home] [Help]

PACKAGE BODY: SYS.HTP

Source


1 package body htp as
2 
3    db_charset constant varchar2(30) :=
4                          substr(userenv('LANGUAGE'),
5                                 instr(userenv('LANGUAGE'),'.')+1);
6    req_charset         varchar2(30) := null;
7 /*
8 **    Add these globals to cache the character set information
9 **    used in setHTTPCharset(). The character set IDs are used
10 **    for comparison to determine if conversion is required.
11 */
12    db_charset_ID  pls_integer := 0;
13    nc_charset varchar2(30);
14    nc_charset_ID  pls_integer := 0;
15    ht_charset varchar2(30) := db_charset;
16    ht_charset_ID  pls_integer := db_charset_ID;
17    last_iana_charset varchar(40) := null;
18    got_ht_charset boolean := FALSE;
19 
20    NL_CHAR   constant varchar2(10) := owa_cx.nl_char;
21    NLNL_CHAR constant varchar2(10) := NL_CHAR||NL_CHAR;
22 
23    htcurline  varchar2(256) := ''; -- htbuf_arr element size
24    htbuf      htbuf_arr;
25    rows_in    pls_integer;
26    rows_out   pls_integer;
27    --
28    -- pack_after :
29    -- Legacy constant used by OAS to denote the line after which htbuf rows
30    -- should be packed. This constant was being used to avoid packing multiple
31    -- HTTP header lines in the same row. We now optimize such that we start
32    -- packing as soon as we see the end of headers.
33    -- Refer to bug#2609772 for more details on packing related issues
34    --
35    pack_after number := 60;
36 
37    htraws     htraw_arr;
38    RAW_MAX    constant pls_integer := 256;
39    bRawMode   boolean := false;
40    contentLen pls_integer := 0;
41 
42    sContentType        constant varchar2(16) := 'CONTENT-TYPE:';
43    sContentLength      constant varchar2(16) := 'CONTENT-LENGTH:';
44    sLocation           constant varchar2(16) := 'LOCATION:';
45    sStatus             constant varchar2(16) := 'STATUS:';
46    sSetCookie          constant varchar2(16) := 'SET-COOKIE:';
47    sTextHtml           constant varchar2(16) := 'text/html';
48 
49    nContentTypeLen     constant number := length(sContentType);
50    nContentLengthLen   constant number := length(sContentLength);
51    nLocationLen        constant number := length(sLocation);
52    nStatusLen          constant number := length(sStatus);
53    nSetCookieLen       constant number := length(sSetCookie);
54 
55    bAddDefaultHTMLHdr  boolean := TRUE;
56    bHTMLPageReady      boolean := FALSE;
57    bHasContentLength   boolean := FALSE;
58    nEndOfHdrIx         pls_integer := -1;
59    nContentLengthIx    binary_integer := -1;
60 
61    sDownloadFilesList     varchar2(256); -- for file download feature
62    nCompressDownloadFiles binary_integer;
63 
64    bFirstCall          boolean := TRUE;
65    nGatewayVersion     pls_integer := 0;       /* Unknown gateway */
66 
67    -- Forward declaration of local procedure
68    procedure prn_raw(cbuf in varchar2 character set any_cs);
69 
70 /* STRUCTURE tags */
71 procedure htmlOpen is
72 begin p(htf.htmlOpen); end;
73 
74 procedure htmlClose is
75 begin p(htf.htmlClose); end;
76 
77 procedure headOpen is
78 begin p(htf.headOpen); end;
79 
80 procedure headClose is
81 begin p(htf.headClose); end;
82 
83 procedure bodyOpen(cbackground in varchar2 DEFAULT NULL,
84                    cattributes in varchar2 DEFAULT NULL) is
85 begin p(htf.bodyOpen(cbackground,cattributes)); end;
86 
87 procedure bodyClose is
88 begin p(htf.bodyClose); end;
89 /* END STRUCTURE tags */
90 
91 /* HEAD Related elements tags */
92 procedure title  (ctitle in varchar2 character set any_cs) is
93 begin p(htf.title(ctitle)); end;
94 
95 procedure htitle(ctitle      in varchar2 character set any_cs,
96                  nsize       in integer  DEFAULT 1,
97                  calign      in varchar2 DEFAULT NULL,
98                  cnowrap     in varchar2 DEFAULT NULL,
99                  cclear      in varchar2 DEFAULT NULL,
100                  cattributes in varchar2 DEFAULT NULL) is
101 begin p(htf.htitle(ctitle,nsize,calign,cnowrap,cclear,cattributes)); end;
102 
103 procedure base(    ctarget     in varchar2 DEFAULT NULL,
104         cattributes    in varchar2 DEFAULT NULL) is
105 begin p(htf.base(ctarget,cattributes)); end;
106 
107 procedure isindex(cprompt in varchar2 character set any_cs DEFAULT NULL,
108                   curl    in varchar2  DEFAULT NULL) is
109 begin p(htf.isindex(cprompt, curl)); end;
110 
111 procedure linkRel(crel   in varchar2,
112                   curl   in varchar2,
113                   ctitle in varchar2 character set any_cs DEFAULT NULL) is
114 begin p(htf.linkRel(crel, curl, ctitle)); end;
115 
116 procedure linkRev(crev   in varchar2,
117                   curl   in varchar2,
118                   ctitle in varchar2 character set any_cs DEFAULT NULL) is
119 begin p(htf.linkRev(crev, curl, ctitle)); end;
120 
121 procedure meta(chttp_equiv in varchar2,
122                cname       in varchar2,
123                ccontent    in varchar2) is
124 begin p(htf.meta(chttp_equiv, cname, ccontent)); end;
125 
126 procedure nextid(cidentifier in varchar2) is
127 begin p(htf.nextid(cidentifier)); end;
128 
129 procedure style(cstyle in varchar2 character set any_cs) is
130 begin p(htf.style(cstyle)); end;
131 
132 procedure script(cscript     in varchar2,
133                  clanguage   in varchar2 DEFAULT NULL) is
134 begin p(htf.script(cscript, clanguage)); end;
135 
136 /* END HEAD Related elements tags */
137 
138 /* BODY ELEMENT tags */
139 procedure hr  (cclear      in varchar2 DEFAULT NULL,
140                csrc        in varchar2 DEFAULT NULL,
141                cattributes in varchar2 DEFAULT NULL) is
142 begin p(htf.hr(cclear, csrc, cattributes)); end;
143 
144 procedure line(cclear      in varchar2 DEFAULT NULL,
145                csrc        in varchar2 DEFAULT NULL,
146                cattributes in varchar2 DEFAULT NULL) is
147 begin htp.hr(cclear, csrc, cattributes); end;
148 
149 procedure nl  (cclear      in varchar2 DEFAULT NULL,
150                cattributes in varchar2 DEFAULT NULL) is
151 begin p(htf.nl(cclear,cattributes)); end;
152 
153 procedure br  (cclear      in varchar2 DEFAULT NULL,
154                cattributes in varchar2 DEFAULT NULL) is
155 begin htp.nl(cclear,cattributes); end;
156 
157 procedure header(nsize   in integer,
158                  cheader in varchar2 character set any_cs,
159                  calign  in varchar2 DEFAULT NULL,
160                  cnowrap in varchar2 DEFAULT NULL,
161                  cclear  in varchar2 DEFAULT NULL,
162                  cattributes in varchar2 DEFAULT NULL) is
163 begin p(htf.header(nsize,cheader,calign,cnowrap,cclear,cattributes)); end;
164 
165 procedure anchor(curl        in varchar2,
166                  ctext       in varchar2 character set any_cs,
167                  cname       in varchar2 DEFAULT NULL,
168                  cattributes in varchar2 DEFAULT NULL) is
169 begin p(htf.anchor(curl,ctext,cname,cattributes)); end;
170 
171 procedure anchor2(curl       in varchar2,
172                  ctext       in varchar2 character set any_cs,
173                  cname       in varchar2 DEFAULT NULL,
174                  ctarget     in varchar2 DEFAULT NULL,
175                  cattributes in varchar2 DEFAULT NULL) is
176 begin p(htf.anchor2(curl,ctext,cname,ctarget,cattributes)); end;
177 
178 procedure mailto(caddress    in varchar2,
179                  ctext       in varchar2 character set any_cs,
180                  cname       in varchar2 character set any_cs DEFAULT NULL,
181                  cattributes in varchar2 DEFAULT NULL) is
182 begin p(htf.mailto(caddress,ctext,cname,cattributes)); end;
183 
184 procedure img(curl        in varchar2,
185               calign      in varchar2 DEFAULT NULL,
186               calt        in varchar2 DEFAULT NULL,
187               cismap      in varchar2 DEFAULT NULL,
188               cattributes in varchar2 DEFAULT NULL) is
189 begin p(htf.img(curl,calign,calt,cismap,cattributes)); end;
190 
191 procedure img2(curl       in varchar2,
192               calign      in varchar2 DEFAULT NULL,
193               calt        in varchar2 DEFAULT NULL,
194               cismap      in varchar2 DEFAULT NULL,
195               cusemap     in varchar2 DEFAULT NULL,
196               cattributes in varchar2 DEFAULT NULL) is
197 begin p(htf.img2(curl,calign,calt,cismap,cusemap,cattributes)); end;
198 
199 procedure area(    ccoords  in varchar2,
200                   cshape    in varchar2 DEFAULT NULL,
201                   chref     in varchar2 DEFAULT NULL,
202                  cnohref    in varchar2 DEFAULT NULL,
203         ctarget in varchar2 DEFAULT NULL,
204         cattributes in varchar2 DEFAULT NULL) is
205 begin p(htf.area(ccoords,cshape,chref,cnohref,ctarget,cattributes));end;
206 
207 procedure mapOpen(cname    in varchar2,cattributes in varchar2 DEFAULT NULL) is
208 begin p(htf.mapOpen(cname,cattributes)); end;
209 procedure mapClose is
210 begin p(htf.mapClose); end;
211 
212 procedure bgsound(csrc    in varchar2,
213           cloop    in varchar2 DEFAULT NULL,
214           cattributes in varchar2 DEFAULT NULL) is
215 begin p(htf.bgsound(csrc,cloop,cattributes));end;
216 
217 procedure para is
218 begin p(htf.para); end;
219 
220 procedure paragraph(calign       in varchar2 DEFAULT NULL,
221                     cnowrap      in varchar2 DEFAULT NULL,
222                     cclear       in varchar2 DEFAULT NULL,
223                     cattributes  in varchar2 DEFAULT NULL) is
224 begin p(htf.paragraph(calign,cnowrap,cclear,cattributes)); end;
225 
226 procedure div(    calign       in varchar2 DEFAULT NULL,
227                 cattributes  in varchar2 DEFAULT NULL) is
228 begin p(htf.div(calign,cattributes)); end;
229 
230 procedure address(cvalue       in varchar2 character set any_cs,
231                   cnowrap      in varchar2 DEFAULT NULL,
232                   cclear       in varchar2 DEFAULT NULL,
233                   cattributes  in varchar2 DEFAULT NULL) is
234 begin p(htf.address(cvalue, cnowrap, cclear, cattributes)); end;
235 
236 procedure comment(ctext in varchar2 character set any_cs) is
237 begin p(htf.comment(ctext)); end;
238 
239 procedure preOpen(cclear      in varchar2 DEFAULT NULL,
240                   cwidth      in varchar2 DEFAULT NULL,
241                   cattributes in varchar2 DEFAULT NULL) is
242 begin p(htf.preOpen(cclear,cwidth,cattributes)); end;
243 
244 procedure preClose is
245 begin p(htf.preClose); end;
246 
247 procedure listingOpen is
248 begin p(htf.listingOpen); end;
249 procedure listingClose is
250 begin p(htf.listingClose); end;
251 
252 procedure nobr(ctext in varchar2 character set any_cs) is
253 begin p(htf.nobr(ctext)); end;
254 procedure wbr is
255 begin p(htf.wbr); end;
256 
257 procedure center(ctext in varchar2 character set any_cs) is
258 begin p(htf.center(ctext)); end;
259 
260 procedure centerOpen is
261 begin p(htf.centerOpen); end;
262 
263 procedure centerClose is
264 begin p(htf.centerClose); end;
265 
266 
267 
268 procedure blockquoteOpen(cnowrap      in varchar2 DEFAULT NULL,
269                          cclear       in varchar2 DEFAULT NULL,
270                          cattributes  in varchar2 DEFAULT NULL) is
271 begin p(htf.blockquoteOpen(cnowrap,cclear,cattributes)); end;
272 
273 procedure blockquoteClose is
274 begin p(htf.blockquoteClose); end;
275 
276 /* LIST tags */
277 procedure listHeader(ctext in varchar2 character set any_cs,
278                      cattributes in varchar2 DEFAULT NULL) is
279 begin p(htf.listHeader(ctext,cattributes)); end;
280 
281 procedure listItem(ctext       in varchar2 character set any_cs DEFAULT NULL,
282                    cclear      in varchar2 DEFAULT NULL,
283                    cdingbat    in varchar2 DEFAULT NULL,
284                    csrc        in varchar2 DEFAULT NULL,
285                    cattributes in varchar2 DEFAULT NULL) is
286 begin p(htf.listItem(ctext,cclear,cdingbat,csrc,cattributes)); end;
287 
288 procedure ulistOpen(cclear      in varchar2 DEFAULT NULL,
289                     cwrap       in varchar2 DEFAULT NULL,
290                     cdingbat    in varchar2 DEFAULT NULL,
291                     csrc        in varchar2 DEFAULT NULL,
292                     cattributes in varchar2 DEFAULT NULL) is
293 begin p(htf.ulistOpen(cclear,cwrap,cdingbat,csrc,cattributes)); end;
294 
295 procedure ulistClose is
296 begin p(htf.ulistClose); end;
297 
298 procedure olistOpen(cclear      in varchar2 DEFAULT NULL,
299                     cwrap       in varchar2 DEFAULT NULL,
300                     cattributes in varchar2 DEFAULT NULL) is
301 begin p(htf.olistOpen(cclear,cwrap,cattributes)); end;
302 
303 procedure olistClose is
304 begin p(htf.olistClose); end;
305 
306 procedure dlistOpen(cclear      in varchar2 DEFAULT NULL,
307                     cattributes in varchar2 DEFAULT NULL) is
308 begin p(htf.dlistOpen(cclear,cattributes)); end;
309 
310 procedure dlistTerm(ctext       in varchar2 character set any_cs DEFAULT NULL,
311                     cclear      in varchar2 DEFAULT NULL,
312                     cattributes in varchar2 DEFAULT NULL) is
313 begin p(htf.dlistTerm(ctext,cclear,cattributes)); end;
314 
315 procedure dlistDef(ctext       in varchar2 character set any_cs DEFAULT NULL,
316                    cclear      in varchar2 DEFAULT NULL,
317                    cattributes in varchar2 DEFAULT NULL) is
318 begin p(htf.dlistDef(ctext,cclear,cattributes)); end;
319 
320 procedure dlistClose is
321 begin p(htf.dlistClose); end;
322 
323 procedure menulistOpen is
324 begin p(htf.menulistOpen); end;
325 
326 procedure menulistClose is
327 begin p(htf.menulistClose); end;
328 
329 procedure dirlistOpen is
330 begin p(htf.dirlistOpen); end;
331 
332 procedure dirlistClose is
333 begin p(htf.dirlistClose); end;
334 /* END LIST tags */
335 
336 /* SEMANTIC FORMAT ELEMENTS */
337 procedure dfn(ctext in varchar2 character set any_cs,
338                cattributes in varchar2 DEFAULT NULL) is
339 begin p(htf.dfn(ctext,cattributes)); end;
340 
344 
341 procedure cite(ctext in varchar2 character set any_cs,
342                cattributes in varchar2 DEFAULT NULL) is
343 begin p(htf.cite(ctext,cattributes)); end;
345 procedure code(ctext in varchar2 character set any_cs,
346                cattributes in varchar2 DEFAULT NULL) is
347 begin p(htf.code(ctext,cattributes)); end;
348 
349 procedure em(ctext  in varchar2 character set any_cs,
350              cattributes in varchar2 DEFAULT NULL) is
351 begin p(htf.em(ctext,cattributes)); end;
352 
353 procedure emphasis(ctext in varchar2 character set any_cs,
354                    cattributes in varchar2 DEFAULT NULL) is
355 begin p(htf.emphasis(ctext,cattributes)); end;
356 
357 procedure kbd(ctext in varchar2 character set any_cs,
358               cattributes in varchar2 DEFAULT NULL) is
359 begin p(htf.kbd(ctext,cattributes)); end;
360 
361 procedure keyboard(ctext in varchar2 character set any_cs,
362                    cattributes in varchar2 DEFAULT NULL) is
363 begin p(htf.keyboard(ctext,cattributes)); end;
364 
365 procedure sample(ctext in varchar2 character set any_cs,
366                  cattributes in varchar2 DEFAULT NULL) is
367 begin p(htf.sample(ctext,cattributes)); end;
368 
369 procedure strong (ctext  in varchar2 character set any_cs,
370                   cattributes in varchar2 DEFAULT NULL) is
371 begin p(htf.strong(ctext,cattributes)); end;
372 
373 procedure variable(ctext in varchar2 character set any_cs,
374                    cattributes in varchar2 DEFAULT NULL) is
375 begin p(htf.variable(ctext,cattributes)); end;
376 
377 procedure big(    ctext          in varchar2 character set any_cs,
378                   cattributes    in varchar2 DEFAULT NULL) is
379 begin p(htf.big(ctext,cattributes)); end;
380 
381 procedure small(ctext          in varchar2 character set any_cs,
382                 cattributes     in varchar2 DEFAULT NULL) is
383 begin p(htf.small(ctext,cattributes)); end;
384 
385 procedure sub(    ctext          in varchar2 character set any_cs,
386                   calign         in varchar2 DEFAULT NULL,
387                   cattributes    in varchar2 DEFAULT NULL) is
388 begin p(htf.sub(ctext,calign,cattributes)); end;
389 
390 procedure sup(    ctext         in varchar2 character set any_cs,
391                   calign        in varchar2 DEFAULT NULL,
392                   cattributes   in varchar2 DEFAULT NULL) is
393 begin p(htf.sup(ctext,calign,cattributes)); end;
394 
395 
396 /* END SEMANTIC FORMAT ELEMENTS */
397 
398 /* PHYSICAL FORMAT ELEMENTS */
399 procedure basefont(nsize in integer) is
400 begin p(htf.basefont(nsize));end;
401 
402 procedure fontOpen(ccolor    in varchar2 DEFAULT NULL,
403            cface    in varchar2 DEFAULT NULL,
404            csize     in varchar2 DEFAULT NULL,
405            cattributes    in varchar2 DEFAULT NULL) is
406 begin p(htf.fontOpen(ccolor,cface,csize,cattributes)); end;
407 
408 procedure fontClose is
409 begin p(htf.fontClose); end;
410 
411 procedure bold   (ctext  in varchar2 character set any_cs,
412                   cattributes in varchar2 DEFAULT NULL) is
413 begin p(htf.bold(ctext,cattributes)); end;
414 
415 procedure italic (ctext  in varchar2 character set any_cs,
416                   cattributes in varchar2 DEFAULT NULL) is
417 begin p(htf.italic(ctext,cattributes)); end;
418 
419 procedure teletype(ctext in varchar2 character set any_cs,
420                    cattributes in varchar2 DEFAULT NULL) is
421 begin p(htf.teletype(ctext,cattributes)); end;
422 
423 procedure plaintext(ctext  in varchar2 character set any_cs,
424                     cattributes in varchar2 DEFAULT NULL) is
425 begin p(htf.plaintext(ctext,cattributes)); end;
426 
427 procedure s(ctext  in varchar2 character set any_cs,
428             cattributes in varchar2 DEFAULT NULL) is
429 begin p(htf.s(ctext,cattributes)); end;
430 
431 procedure strike (ctext  in varchar2 character set any_cs,
432                   cattributes in varchar2 DEFAULT NULL) is
433 begin p(htf.strike(ctext,cattributes)); end;
434 
435 procedure underline (ctext  in varchar2 character set any_cs,
436                   cattributes in varchar2 DEFAULT NULL) is
437 begin p(htf.underline(ctext,cattributes)); end;
438 
439 /* END PHYSICAL FORMAT ELEMENTS */
440 
441 /* HTML FORMS */
442 
443 procedure formOpen(curl     in varchar2,
444                    cmethod  in varchar2 DEFAULT 'post',
445            ctarget  in varchar2 DEFAULT NULL,
446            cenctype in varchar2 DEFAULT NULL,
447            cattributes in varchar2 DEFAULT NULL) is
448 begin p(htf.formOpen(curl,cmethod,ctarget,cenctype,cattributes)); end;
449 
450 procedure formCheckbox(cname       in varchar2,
451                       cvalue      in varchar2 character set any_cs DEFAULT 'on',
452                        cchecked    in varchar2 DEFAULT NULL,
453                        cattributes in varchar2 DEFAULT NULL) is
454 begin p(htf.formCheckbox(cname,cvalue,cchecked,cattributes)); end;
455 
456 procedure formFile(cname       in varchar2,
457                    caccept     in varchar2 DEFAULT NULL,
458                    cattributes in varchar2 DEFAULT NULL) is
459 begin p(htf.formFile(cname,caccept,cattributes)); end;
460 
461 procedure formHidden(cname       in varchar2,
462                      cvalue      in varchar2 character set any_cs DEFAULT NULL,
466 procedure formImage(cname       in varchar2,
463                      cattributes in varchar2 DEFAULT NULL) is
464 begin p(htf.formHidden(cname,cvalue,cattributes)); end;
465 
467                     csrc        in varchar2,
468                     calign      in varchar2 DEFAULT NULL,
469                     cattributes in varchar2 DEFAULT NULL) is
470 begin p(htf.formImage(cname,csrc,calign,cattributes)); end;
471 
472 procedure formPassword(cname       in varchar2,
473                        csize       in varchar2 DEFAULT NULL,
474                        cmaxlength  in varchar2 DEFAULT NULL,
475                        cvalue      in varchar2 character set any_cs DEFAULT NULL,
476                        cattributes in varchar2 DEFAULT NULL) is
477 begin p(htf.formPassword(cname,csize,cmaxlength,cvalue,cattributes)); end;
478 
479 procedure formRadio(cname       in varchar2,
480                     cvalue      in varchar2 character set any_cs,
481                     cchecked    in varchar2 DEFAULT NULL,
482                     cattributes in varchar2 DEFAULT NULL) is
483 begin p(htf.formRadio(cname,cvalue,cchecked,cattributes)); end;
484 
485 procedure formReset(cvalue     in varchar2 character set any_cs DEFAULT 'Reset',
486                    cattributes in varchar2 DEFAULT NULL) is
487 begin p(htf.formReset(cvalue,cattributes)); end;
488 
489 procedure formSubmit(cname       in varchar2 DEFAULT NULL,
490                   cvalue      in varchar2 character set any_cs DEFAULT 'Submit',
491                   cattributes in varchar2 DEFAULT NULL) is
492 begin p(htf.formSubmit(cname,cvalue,cattributes)); end;
493 
494 procedure formText(cname       in varchar2,
495                    csize       in varchar2 DEFAULT NULL,
496                    cmaxlength  in varchar2 DEFAULT NULL,
497                    cvalue      in varchar2 character set any_cs DEFAULT NULL,
498                    cattributes in varchar2 DEFAULT NULL) is
499 begin p(htf.formText(cname,csize,cmaxlength,cvalue,cattributes)); end;
500 
501 procedure formSelectOpen(cname       in varchar2,
502                       cprompt     in varchar2 character set any_cs DEFAULT NULL,
503                          nsize       in integer  DEFAULT NULL,
504                          cattributes in varchar2 DEFAULT NULL) is
505 begin p(htf.formSelectOpen(cname,cprompt,nsize,cattributes)); end;
506 
507 procedure formSelectOption(cvalue      in varchar2 character set any_cs,
508                            cselected   in varchar2 DEFAULT NULL,
509                            cattributes in varchar2 DEFAULT NULL) is
510 begin p(htf.formSelectOption(cvalue,cselected,cattributes)); end;
511 
512 procedure formSelectClose is
513 begin p(htf.formSelectClose); end;
514 
515 procedure formTextarea(cname       in varchar2,
516                        nrows       in integer,
517                        ncolumns    in integer,
518                        calign      in varchar2 DEFAULT NULL,
519                        cattributes in varchar2 DEFAULT NULL) is
520 begin p(htf.formTextarea(cname,nrows,ncolumns,calign,cattributes)); end;
521 
522 
523 procedure formTextarea2(cname       in varchar2,
524                        nrows       in integer,
525                        ncolumns    in integer,
526                        calign      in varchar2 DEFAULT NULL,
527                        cwrap       in varchar2 DEFAULT NULL,
528                        cattributes in varchar2 DEFAULT NULL) is
529 begin p(htf.formTextarea2(cname,nrows,ncolumns,calign,cwrap,cattributes)); end;
530 
531 
532 procedure formTextareaOpen(cname       in varchar2,
533                            nrows       in integer,
534                            ncolumns    in integer,
535                            calign      in varchar2 DEFAULT NULL,
536                            cattributes in varchar2 DEFAULT NULL) is
537 begin p(htf.formTextareaOpen(cname,nrows,ncolumns,calign,cattributes)); end;
538 
539 
540 procedure formTextareaOpen2(cname       in varchar2,
541                            nrows       in integer,
542                            ncolumns    in integer,
543                            calign      in varchar2 DEFAULT NULL,
544                            cwrap       in varchar2 DEFAULT NULL,
545                            cattributes in varchar2 DEFAULT NULL) is
546 begin
547    p(htf.formTextareaOpen2(cname,nrows,ncolumns,calign,cwrap,cattributes));
548 end;
549 
550 procedure formTextareaClose is
551 begin p(htf.formTextareaClose); end;
552 
553 procedure formClose is
554 begin p(htf.formClose); end;
555 /* END HTML FORMS */
556 
557 /* HTML TABLES */
558 procedure tableOpen(cborder in varchar2 DEFAULT NULL,
559                    calign in varchar2 DEFAULT NULL,
560                    cnowrap in varchar2 DEFAULT NULL,
561                    cclear in varchar2 DEFAULT NULL,
562                    cattributes in varchar2 DEFAULT NULL) is
563 begin p(htf.tableOpen(cborder,calign,cnowrap,cclear,cattributes)); end;
564 
565 procedure tableCaption(ccaption    in varchar2 character set any_cs,
566                        calign      in varchar2 DEFAULT NULL,
567                        cattributes in varchar2 DEFAULT NULL) is
568 begin p(htf.tableCaption(ccaption,calign,cattributes)); end;
569 
570 procedure tableRowOpen(calign      in varchar2 DEFAULT NULL,
571                        cvalign     in varchar2 DEFAULT NULL,
575 begin p(htf.tableRowOpen(calign,cvalign,cdp,cnowrap,cattributes)); end;
572                        cdp         in varchar2 DEFAULT NULL,
573                        cnowrap     in varchar2 DEFAULT NULL,
574                        cattributes in varchar2 DEFAULT NULL) is
576 
577 procedure tableHeader(cvalue      in varchar2 character set any_cs DEFAULT NULL,
578                       calign      in varchar2 DEFAULT NULL,
579                       cdp         in varchar2 DEFAULT NULL,
580                       cnowrap     in varchar2 DEFAULT NULL,
581                       crowspan    in varchar2 DEFAULT NULL,
582                       ccolspan    in varchar2 DEFAULT NULL,
583                       cattributes in varchar2 DEFAULT NULL) is
584 begin p(htf.tableHeader(cvalue,calign,cdp,cnowrap,
585                         crowspan,ccolspan,cattributes)); end;
586 
587 procedure tableData(cvalue      in varchar2 character set any_cs DEFAULT NULL,
588                     calign      in varchar2 DEFAULT NULL,
589                     cdp         in varchar2 DEFAULT NULL,
590                     cnowrap     in varchar2 DEFAULT NULL,
591                     crowspan    in varchar2 DEFAULT NULL,
592                     ccolspan    in varchar2 DEFAULT NULL,
593                     cattributes in varchar2 DEFAULT NULL) is
594 begin p(htf.tableData(cvalue,calign,cdp,cnowrap,
595                       crowspan,ccolspan,cattributes)); end;
596 
597 procedure tableRowClose is
598 begin p(htf.tableRowClose); end;
599 
600 procedure tableClose is
601 begin p(htf.tableClose); end;
602 /* END HTML TABLES */
603 
604 /* BEGIN HTML FRAMES - Netscape Extensions FRAMESET, FRAME tags */
605 procedure framesetOpen(    crows    in varchar2 DEFAULT NULL,    /* row height value list */
606             ccols    in varchar2 DEFAULT NULL,
607             cattributes in varchar2 DEFAULT NULL) is    /* column width list */
608 begin
609  p(htf.framesetOpen( crows, ccols, cattributes ));
610 end framesetOpen;
611 
612 procedure framesetClose is
613 begin
614  p(htf.framesetClose);
615 end framesetClose;
616 
617 procedure frame(    csrc    in varchar2,                /* URL */
618             cname    in varchar2 DEFAULT NULL,        /* Window Name */
619             cmarginwidth     in varchar2 DEFAULT NULL,    /* Value in pixels */
620             cmarginheight    in varchar2 DEFAULT NULL,    /* Value in pixels */
621             cscrolling    in varchar2 DEFAULT NULL,    /* yes | no | auto */
622             cnoresize    in varchar2 DEFAULT NULL,
623             cattributes     in varchar2 DEFAULT NULL) is    /* Not resizable by user */
624 begin
625  p(htf.frame( csrc, cname, cmarginwidth, cmarginheight, cscrolling, cnoresize, cattributes ));
626 end frame;
627 
628 procedure noframesOpen is
629 begin
630  p(htf.noframesOpen);
631 end noframesOpen;
632 
633 procedure noframesClose is
634 begin
635  p(htf.noframesClose);
636 end noframesClose;
637 
638 /* END HTML FRAMES */
639 
640 /* SPECIAL HTML TAGS */
641 procedure appletOpen(    ccode        in varchar2,
642             cwidth        in integer,
643             cheight        in integer,
644             cattributes    in varchar2 DEFAULT NULL) is
645 begin p(htf.appletOpen(ccode,cwidth,cheight,cattributes));end;
646 
647 procedure param(    cname        in varchar2,
648             cvalue        in varchar2 character set any_cs) is
649 begin p(htf.param(cname,cvalue));end;
650 
651 procedure appletClose is
652 begin p(htf.appletClose);end;
653 
654 /* END SPECIAL HTML TAGS */
655 
656 
657 /* SPECIAL PROCEDURES */
658 
659 /* This procedure sets the value of CGI env var REQUEST_CHARSET in a global var */
660 procedure set_request_charset
661 is
662 begin
663     if (req_charset is null)
664     then
665         req_charset := owa_util.get_cgi_env('REQUEST_CHARSET');
666         if (req_charset is null)
667         then
668             -- set req_charset to match DB_CHARSET if not set
669             req_charset := db_charset;
670         end if;
671         req_charset := UPPER(req_charset);
672     end if;
673 end set_request_charset;
674 
675 function getContentLength return number is
676    len      pls_integer := 0;
677    nFromIx  pls_integer;
678 begin
679    -- Check to see if we have a BLOB download, and if so,
680    -- return the length of the BLOB
681    IF (wpg_docload.is_file_download)
682    THEN
683       RETURN wpg_docload.get_content_length;
684    END IF;
685    if (bRawMode) then
686       return contentLen ;
687    end if;
688 
689    nFromIx := nEndOfHdrIx + 1;
690    for nIx in nFromIx..rows_in loop
691      len := len + lengthb(htbuf(nIx)); -- use lengthb to get in bytes
692    end loop;
693    return(len);
694 end getContentLength;
695 
696 /* Start of OAS specific helper procedure */
697 function getContentLength_cs_convert ( charset in varchar2) return number is
698    len      binary_integer := 0;
699    nFromIx  binary_integer;
700 begin
701    nFromIx := nEndOfHdrIx + 1;
702    for nIx in nFromIx..rows_in
703    loop
704       len := len + lengthb( convert (htbuf(nIx), charset));
705    end loop;
706    return(len);
710 procedure init is
707 end getContentLength_cs_convert;
708 /* End of OAS specific helper procedure */
709 
711 begin
712    htcurline := '';
713    rows_in := 0;
714    rows_out := 0;
715    htbuf.delete;
716    pack_after := 60;             /* see comments on 'pack_after' */
717 
718    bAddDefaultHTMLHdr := TRUE;
719    bHTMLPageReady := FALSE;
720    bHasContentLength := FALSE;
721    nEndOfHdrIx := -1;
722    nContentLengthIx := -1;
723 
724    sDownloadFilesList := '';
725    nCompressDownloadFiles := 0;
726 
727    addDefaultHTMLHdr(TRUE);
728    htraws.delete;
729 
730    bRawMode := false;
731    contentLen := 0;
732    got_ht_charset := FALSE;
733 
734    bFirstCall := TRUE;
735    owa_cookie.init;
736    nGatewayVersion := 0;
737 end init;
738 
739 procedure flush_raw is
740 begin
741    if (not bHTMLPageReady)
742    then
743       if (nEndOfHdrIx < 0) -- how come?
744       then
745          nEndOfHdrIx := rows_in;
746       end if;
747       if (nContentLengthIx > 0)
748       then
749          htbuf(nContentLengthIx) := 'Content-length: '
750             || getContentLength || NL_CHAR;
751       end if;
752       bHTMLPageReady := TRUE;
753    end if;
754 end flush_raw;
755 
756 procedure flush is
757 begin
758    set_request_charset;
759    if (htcurline is not null)
760    then
761       rows_in := rows_in + 1;
762       htbuf(rows_in) := htcurline;
763       htcurline := '';
764    end if;
765    if (not bHTMLPageReady)
766    then
767       if (nEndOfHdrIx < 0) -- how come?
768       then
769          nEndOfHdrIx := rows_in;
770       end if;
771       if (nContentLengthIx > 0)
772       then
773          if (db_charset != req_charset)
774          then
775             htbuf(nContentLengthIx) := 'X-DB-Content-length: ' || getContentLength || NL_CHAR;
776          else
777             htbuf(nContentLengthIx) := 'Content-length: '
778                || getContentLength || NL_CHAR;
779          end if;
780       end if;
781       bHTMLPageReady := TRUE;
782    end if;
783 end flush;
784 
785 /* Start of OAS specific helper procedure */
786 procedure flush_charset_convert ( charset in varchar2)  is
787 begin
788    if (htcurline is not null)
789    then
790       rows_in := rows_in + 1;
791       htbuf(rows_in) := htcurline;
792       htcurline := '';
793    end if;
794    if (not bHTMLPageReady)
795    then
796       if (nEndOfHdrIx < 0) -- how come?
797       then
798          nEndOfHdrIx := rows_in;
799       end if;
800       if (nContentLengthIx > 0)
801       then
802          htbuf(nContentLengthIx) := 'Content-length: '
803             || getContentLength_cs_convert ( charset) || NL_CHAR;
804       end if;
805       bHTMLPageReady := TRUE;
806    end if;
807 end flush_charset_convert;
808 /* End of OAS specific helper procedure */
809 
810 function get_line (irows out integer) return varchar2 is
811    cnt      number;
812 begin
813    flush;
814 
815    cnt := rows_in - rows_out;
816 
817    if (cnt > 1)
818    then
819       irows := 1;
820    else
821       irows := 0;
822       if (cnt < 1)
823       then
824          return(NULL);
825       end if;
826    end if;
827 
828    rows_out := rows_out + 1;
829    return(htbuf(rows_out));
830 end;
831 
832 procedure get_page (thepage     out NOCOPY htbuf_arr,
833                     irows    in out integer ) is
834   nrows       integer;
835   tmpbuf      varchar2(256);
836   num_headers pls_integer;
837   len         pls_integer;
838 begin
839 
840    --
841    -- Flush any remaining data from htcurline into htbuf and setup the
842    -- content-length
843    --
844    flush;
845 
846    if (db_charset != req_charset)
847    then
848       irows := least(irows, rows_in - rows_out);
849       if (irows = 0)
850       then
851          return;
852       end if;
853 
854       for i in 1..irows
855       loop
856          thepage(i) := htbuf(rows_out + i);
857       end loop;
858 
859       rows_out := rows_out + irows;
860    else
861       --
862       -- We can completely avoid packing response data in the following situations
863       --
864       -- 1. HTBUF_LEN was not reduced from its default value of 255 (single-byte
865       --    database)
866       --
867       -- 2. the user supplied buffer is large enough to hold the response (we
868       --    are aiming to achieve this by configuring the number of rows
869       --    in the user's buffer
870       --
871       -- This optimization will avoid some string manipulation/concatenation
872       -- operations and some lengthb calls as well
873       --
874       -- We should also avoid packing in situations where we are talking to the
875       -- older gateway (iversion <= 2)
876       --
877 
881 	 -- Rows to fetch = min (size of user supplied buffer, rows_left)
878       if ((HTBUF_LEN = 255) or (irows >= (rows_in - rows_out)) or
879 	  (nGatewayVersion <= 2)) then
880 
882 	 irows := least(irows, rows_in - rows_out);
883 	 if (irows = 0)
884 	 then
885 	    return;
886 	 end if;
887 
888 	 -- Transfer rows to user supplied buffer
889 	 for i in 1..irows
890 	 loop
891 	    thepage(i) := htbuf(rows_out + i);
892 	 end loop;
893 
894 	 -- Update rows_out for next fetch
895 	 rows_out := rows_out + irows;
896 
897 	 return;
898 
899       end if;
900 
901       --
902       -- We need to start packing lines in htbuf so that there are minimal
903       -- round trips from the client while making get_page calls
904       --
905       -- 1. We will not be packing response header lines due to dependencies
906       --    in the PL/SQL Gateway to expect each header line to start in a
907       --    new row
908       --
909       -- 2. Consecutive rows will be packed as long as the total size does
910       --    not exceed the width of a buffer row in bytes. No attempts will be
911       --    made to pack the row to the fullest.
912       --
913       -- 3. If a line is sufficiently packed (200 bytes), we will make no attempt
914       --    to pack anything else in it. The flip side is that we will flush
915       --    tmpbuf in such situations. So, some lines could end up with a
916       --    lot lesser packing.
917       --
918 
919       nrows := 0;
920       tmpbuf := '';
921 
922       -- Do not pack header lines
923       if (rows_out <= nEndOfHdrIx) then
924 	 num_headers := least (irows, nEndOfHdrIx - rows_out);
925 	 for i in 1..num_headers
926 	 loop
927 	    thepage(i) := htbuf(rows_out + i);
928 	 end loop;
929 	 rows_out := rows_out + num_headers;
930 	 nrows := num_headers;
931       end if;
932 
933       while (nrows < irows)
934       loop
935 	 if (rows_out = rows_in) then
936 	    -- No more data in internal buffer, write last row and return
937 	    if (tmpbuf is null) then
938 	       irows := nrows;
939 	    else
940 	       irows := nrows + 1;
941 	       thepage(irows) := tmpbuf;
942 	    end if;
943 	    return;
944 	 end if;
945 
946 	 rows_out := rows_out + 1;
947 
948 	 len := lengthb (htbuf(rows_out));
949 	 if (len >= 200) then
950 
951 	    --
952 	    -- Make no attempt to pack sufficiently packed lines
953 	    -- 1. Copy exisiting data in tmpbuf to a new row in the response
954 	    -- 2. Copy current data row to the next row in the response
955 	    --
956 
957 	    -- Step 1
958 	    if (tmpbuf is not null) then
959 	       nrows := nrows + 1;
960 	       thepage(nrows) := tmpbuf;
961 	       tmpbuf := '';
962 	       if (nrows = irows) then
963 		  rows_out := rows_out - 1;
964 		  return;
965 	       end if;
966 	    end if;
967 
968 	    -- Step 2
969 	    nrows := nrows + 1;
970 	    thepage(nrows) := htbuf(rows_out);
971 
972 	 elsif ((len + lengthb(tmpbuf)) > 255) then
973 
974 	    --
975 	    -- Concatenation would result in buffer overflow
976 	    -- 1. Move tmpbuf to a new row
977 	    -- 2. Setup tmpbuf to the current input row
978 	    --
979 
980 	    -- Step 1
981 	    nrows := nrows + 1;
982 	    thepage(nrows) := tmpbuf;
983 	    if (nrows = irows) then
984 	      rows_out := rows_out - 1;
985 	    else
986 	      -- Step 2
987 	      tmpbuf := htbuf(rows_out);
988 	    end if;
989 	 else
990 
991 	    --
992 	    -- Pack lines into tmpbuf
993 	    -- It is more efficient to pack data in a scalar as opposed to
994 	    -- doing it directly in the array. This is an artifact of how
995 	    -- arrays are implemented using BTree and there is a cost of
996 	    -- log(n) for array access. But, using scalars will add extra
997 	    -- cost while updating the response row with contents of tmpbuf
998 	    --
999 	    tmpbuf := tmpbuf || htbuf(rows_out);
1000 
1001 	 end if;
1002       end loop;
1003    end if;
1004 end;
1005 
1006 procedure get_page_raw (thepage     out NOCOPY htraw_arr,
1007                         irows    in out integer  ) is
1008   i       pls_integer;
1009   j       pls_integer;
1010   k       pls_integer;
1011   loc     pls_integer;
1012   len     pls_integer;
1013   tempraw raw(2000);
1014 begin
1015    flush_raw;
1016    irows := least(irows, rows_in - rows_out);
1017    if (irows > 0) then
1018      J := rows_out;
1019      I := 0;
1020      while (I < irows) loop
1021        J := J + 1;
1022        if (J > nEndOfHdrIx) then
1023           -- If transferring raw data, just copy it
1024           i := i + 1;
1025           thepage(i) := htraws(J);
1026        elsif (ht_charset_ID = db_charset_ID) then
1027           -- if transferring headers with identical charsets, cast it
1028           i := i + 1;
1029           thepage(i) := UTL_RAW.CAST_TO_RAW(htbuf(J));
1030        else
1031           -- otherwise, need to charset-convert the buffer
1035           len := UTL_RAW.LENGTH(tempraw);
1032           tempraw := UTL_RAW.CONVERT(UTL_RAW.CAST_TO_RAW(htbuf(J)),
1033                                      'AMERICAN_AMERICA.'||ht_charset,
1034                                      'AMERICAN_AMERICA.'||db_charset);
1036           if (len <= RAW_MAX) then
1037             -- if the post-converted buffer will fit, then just copy it out
1038             i := i + 1;
1039             thepage(i) := tempraw;
1040           else
1041             -- otherwise, cut it into chunks and copy them up to irows max
1042             loc := 1;
1043             k := i;
1044             while (loc < len) loop
1045               k := k + 1;
1046               thepage(k) := UTL_RAW.SUBSTR(tempraw, loc, RAW_MAX);
1047               loc := loc + RAW_MAX;
1048               if (k = irows) then
1049                 exit;
1050               end if;
1051             end loop;
1052             if (loc < len) then
1053               while (i < irows) loop
1054                 i := i + 1;
1055                 thepage(i) := null;
1056               end loop;
1057               J := J - 1;
1058               exit;
1059             else
1060               i := k;
1061             end if;
1062           end if;
1063        end if;
1064      end loop;
1065      rows_out := j;
1066    end if;
1067 end;
1068 
1069 /* Start of OAS specific helper procedure */
1070 procedure get_page_charset_convert (thepage     out NOCOPY htbuf_arr,
1071                     irows    in out integer ,
1072                     charset  in     varchar2 ) is
1073 begin
1074    flush_charset_convert ( charset);
1075 
1076    irows := least(irows, rows_in - rows_out);
1077    if (irows = 0)
1078    then
1079       return;
1080    end if;
1081 
1082    for i in 1..irows
1083    loop
1084       thepage(i) := htbuf(rows_out + i);
1085    end loop;
1086 
1087    rows_out := rows_out + irows;
1088 end;
1089 /* End of OAS specific helper procedure */
1090 
1091 /*
1092  * Take an arbitrarily long version string separated by '.'s and return
1093  * a number based on the first 2 parts.
1094  * e.g. version_string_to_number('10.2.3.2') = 10.2
1095  */
1096 function version_string_to_number(p_version_string in varchar2)
1097 return number
1098 is
1099   l_index_2nd_dot           pls_integer;
1100 begin
1101   l_index_2nd_dot := instr(p_version_string, '.', 1, 2);
1102   if l_index_2nd_dot = 0 then
1103     l_index_2nd_dot := length(p_version_string)+1;
1104   end if;
1105   return substr(p_version_string, 1, l_index_2nd_dot -1);
1106 end version_string_to_number;
1107 
1108 /*
1109  * Returns true if the the database version is greater than or equal to
1110  * the version passed in. The versions are compared to the least number of
1111  * places of either version. So pass in 9.0 and the db_version is
1112  * 9.0.1.4.0 then the version are compared up to two places and are found
1113  * to be the same to that accuracy and hence the function returns true.
1114  * Only check the first 2 places.
1115  */
1116 function require_db_version (p_required_version in varchar2)
1117 return boolean
1118 is
1119   l_db_version_string       varchar2(30);
1120   l_db_compatibility        varchar2(30);
1121   l_db_number2              number;
1122   l_required_number2        number;
1123 begin
1124   dbms_utility.db_version(l_db_version_string, l_db_compatibility);
1125 
1126   l_db_number2 := version_string_to_number(l_db_version_string);
1127   l_required_number2 := version_string_to_number(p_required_version);
1128 
1129   if l_required_number2 <= l_db_number2 then
1130     return true;
1131   else
1132     return false;
1133   end if;
1134 end require_db_version;
1135 
1136 procedure showpage is
1137    dbms_buf_size integer;
1138    buffer   varchar2(510);    /* size = 255 * 2 */
1139    i        integer;
1140    sp_loc   integer;
1141    nl_loc   integer;
1142 begin
1143    -- On DB version 10.2 and above, dbms_output buffer size can be unlimited
1144    if require_db_version('10.2')
1145    then
1146       dbms_output.enable(buffer_size => NULL);
1147    else
1148       /* First figure out how large to make the dbms_output buffer */
1149       dbms_buf_size := (rows_in - rows_out)*255*2;
1150       if (dbms_buf_size > 1000000)
1151       then
1152          dbms_output.enable(1000000);
1153       else
1154          dbms_output.enable(dbms_buf_size);
1155       end if;
1156    end if;
1157 
1158 
1159    /* Now, loop through, adding lines from htbuf, but   */
1160    /* never getting larger than 510 characters.         */
1161    /* If a newline is found, print everything and clear */
1162    /* the buffer, otherwise, break on the last space    */
1163    /* possible.  If the last space is past 255 chars,   */
1164    /* or there is no space at all, then break on 255.   */
1165    flush;
1166 
1167    buffer := NULL;
1168    i := rows_out + 1;
1169 
1170    while ((i <= rows_in) or (buffer is not null))
1171    loop
1172 
1173       /* Pick up the next row if it exists, and there is enough space */
1174       if ((i <= rows_in) and (nvl(length(buffer),0) <= 255))
1175       then
1176          buffer := buffer || htbuf(i);
1177          i := i + 1;
1178       end if;
1179 
1180       /* Search for the last newline character in the first 255 bytes */
1184          /* Newline not found. Try searching for last space */
1181       nl_loc := instr(substr(buffer,1,255), NL_CHAR, -1);
1182       if (nl_loc = 0)
1183       then
1185          sp_loc := instr(substr(buffer,1,255), ' ', -1);
1186          if (sp_loc = 0)
1187          then
1188             /* Space not found. Write out the first 255 bytes as-is */
1189             dbms_output.put_line(substr(buffer, 1, 255));
1190 
1191             /* Update buffer to contain remaining bytes */
1192             buffer := substr(buffer,256);
1193          else
1194             /* Space found. Write out bytes without the space */
1195             dbms_output.put_line(substr(buffer, 1, sp_loc - 1));
1196 
1197             /* Update buffer to contain remaining bytes */
1198             buffer := substr(buffer, sp_loc + 1);
1199          end if;
1200       else
1201          /* Always strip out the newlines */
1202          /* PUT_LINE will put them in.    */
1203 
1204          /* Newline found. Write out bytes without the newline */
1205          dbms_output.put_line(substr(buffer, 1, nl_loc - 1));
1206 
1207          /* Update buffer to contain remaining bytes */
1208          buffer := substr(buffer, nl_loc + 1);
1209       end if;
1210    end loop;
1211 
1212    rows_out := rows_in;
1213 end;
1214 
1215 procedure reset_get_page is
1216 begin
1217    /* Enhancement#5610575 : allow get_page to start over */
1218    rows_out := 0;
1219 end;
1220 
1221 procedure download_file(sFileName in varchar2,
1222    bCompress in boolean default false) is
1223 begin
1224    if (sDownloadFilesList is NULL)
1225    then
1226       sDownLoadFilesList := sFileName;
1227       if (bCompress)
1228       then
1229          nCompressDownloadFiles := 1;
1230       else
1231          nCompressDownloadFiles := 0;
1232       end if;
1233    end if;
1234 end;
1235 
1236 procedure get_download_files_list(sFilesList out varchar2,
1237    nCompress out binary_integer) is
1238 begin
1239    sFilesList := sDownloadFilesList;
1240    nCompress := nCompressDownloadFiles;
1241 end;
1242 
1243 function isHTMLHdr(cbuf in varchar2 character set any_cs) return boolean is
1244    len number := length(cbuf);
1245 begin
1246    return
1247       ((len >= nContentTypeLen
1248            and sContentType = substr(cbuf, 1, nContentTypeLen))
1249     or (len >= nContentLengthLen
1250            and sContentLength = substr(cbuf, 1, nContentLengthLen))
1251     or (len >= nLocationLen
1252            and sLocation = substr(cbuf, 1, nLocationLen))
1253     or (len >= nStatusLen
1254            and sStatus = substr(cbuf, 1, nStatusLen))
1255     or (len >= nSetCookieLen
1256            and sSetCookie = substr(cbuf, 1, nSetCookieLen))
1257       );
1258 end isHTMLHdr;
1259 
1260 procedure addDefaultHTMLHdr(bAddHTMLHdr boolean) is
1261 begin
1262    bAddDefaultHTMLHdr := bAddHTMLHdr;
1263 end addDefaultHTMLHdr;
1264 
1265 /* Enable raw mode transfers */
1266 procedure set_transfer_mode(tmode in varchar2) is
1267 begin
1268   if (lower(tmode) = 'raw')
1269   then
1270     bRawMode := true;
1271   end if;
1272 end set_transfer_mode;
1273 
1274 procedure setHTTPCharset(iana_charset in varchar2,
1275                          ora_charset varchar2 default NULL) is
1276   lower_cs varchar2(40);
1277 begin
1278 
1279   if (iana_charset is null)
1280   then
1281     last_iana_charset := null; -- Invalidate the cache.
1282     ht_charset_ID := NLS_CHARSET_ID(db_charset); -- Get ID for fast comparison
1283     ht_charset := db_charset;
1284     got_ht_charset := TRUE;
1285     return;
1286   end if;
1287 
1288   lower_cs := lower(iana_charset);
1289   if (last_iana_charset is not null) and (lower_cs = last_iana_charset)
1290   then
1291      got_ht_charset := TRUE;
1292      return;
1293   end if;
1294 
1295   if (ora_charset is not null) then ht_charset := ora_charset;
1296   elsif (lower_cs = 'iso-8859-1') then
1297     if (db_charset = 'WE8MSWIN1252')
1298     then ht_charset := db_charset;
1299     else ht_charset := 'WE8ISO8859P1';
1300     end if;
1301   elsif (lower_cs = 'utf-8') then
1302     if (db_charset = 'UTF8')
1303     then ht_charset := db_charset;
1304     else  ht_charset := 'AL32UTF8';
1305     end if;
1306   elsif (lower_cs = 'windows-1252') then
1307     if (db_charset = 'WE8ISO8859P1')
1308     then ht_charset := db_charset;
1309     else ht_charset := 'WE8MSWIN1252';
1310     end if;
1311   elsif (lower_cs = 'us-ascii') then      ht_charset := 'US7ASCII';
1312   elsif (lower_cs = 'iso-8859-2') then
1313     if (db_charset = 'EE8MSWIN1250')
1314     then ht_charset := db_charset;
1315     ht_charset := 'EE8ISO8859P2';
1316     end if;
1317   elsif (lower_cs = 'iso-8859-3') then ht_charset := 'SE8ISO8859P3';
1318   elsif (lower_cs = 'iso-8859-4') then
1319     if (db_charset = 'BLT8MSWIN1257')
1320     then ht_charset := db_charset;
1321     else ht_charset := 'NEE8ISO8859P4';
1322     end if;
1323   elsif (lower_cs = 'iso-8859-5') then
1324     if (db_charset = 'CL8MSWIN1251')
1325     then ht_charset := db_charset;
1326     else   ht_charset := 'CL8ISO8859P5';
1327     end if;
1328   elsif (lower_cs = 'iso-8859-6') then
1329     if (db_charset = 'AR8MSWIN1256')
1330     then ht_charset := db_charset;
1331     else ht_charset := 'AR8ISO8859P6';
1335     then ht_charset := db_charset;
1332     end if;
1333   elsif (lower_cs = 'iso-8859-7') then
1334     if (db_charset = 'EL8MSWIN1253')
1336     else ht_charset := 'EL8ISO8859P7';
1337     end if;
1338   elsif (lower_cs = 'iso-8859-8-i') then
1339     if (db_charset = 'IW8MSWIN1255')
1340     then ht_charset := db_charset;
1341     else ht_charset := 'IW8ISO8859P8';
1342     end if;
1343   elsif (lower_cs = 'iso-8859-9') then    ht_charset := 'WE9ISO8859P9';
1344   elsif (lower_cs = 'iso-8859-10') then   ht_charset := 'NE8ISO8859P10';
1345   elsif (lower_cs = 'shift_jis') then     ht_charset := 'JA16SJIS';
1346   elsif (lower_cs = 'gb2312') or (lower_cs = 'gbk') then
1347     if (db_charset = 'ZHS16CGB231280')
1348     then ht_charset := db_charset;
1349     else ht_charset := 'ZHS16GBK';
1350     end if;
1351   elsif (lower_cs = 'big5') then          ht_charset := 'ZHT16BIG5';
1352   elsif (lower_cs = 'ks_c_5601-1987') or (lower_cs = 'euc-kr') then
1353     if (db_charset = 'KO16MSWIN949')
1354     then ht_charset := db_charset;
1355     else ht_charset := 'KO16KSC5601';
1356     end if;
1357   elsif (lower_cs = 'tis-620') then       ht_charset := 'TH8TISASCII';
1358   elsif (lower_cs = 'euc-jp') then        ht_charset := 'JA16EUC';
1359   elsif (lower_cs = 'windows-1256') then  ht_charset := 'AR8MSWIN1256';
1360   elsif (lower_cs = 'windows-1257') then  ht_charset := 'BLT8MSWIN1257';
1361   elsif (lower_cs = 'windows-1251') then  ht_charset := 'CL8MSWIN1251';
1362   elsif (lower_cs = 'windows-1250') then  ht_charset := 'EE8MSWIN1250';
1363   elsif (lower_cs = 'windows-1253') then  ht_charset := 'EL8MSWIN1253';
1364   elsif (lower_cs = 'windows-1255') then  ht_charset := 'IW8MSWIN1255';
1365   elsif (lower_cs = 'windows-1254') then  ht_charset := 'TR8MSWIN1254';
1366   elsif (lower_cs = 'windows-1258') then  ht_charset := 'VN8MSWIN1258';
1367   elsif (lower_cs = 'windows-921') then   ht_charset := 'LT8MSWIN921';
1368   elsif (lower_cs = 'windows-936') then   ht_charset := 'ZHS16GBK';
1369   elsif (lower_cs = 'windows-950') then   ht_charset := 'ZHT16MSWIN950';
1370   elsif (lower_cs = 'windows-949') then   ht_charset := 'KO16MSWIN949';
1371   elsif (lower_cs = 'koi8-r') then   ht_charset := 'CL8KOI8R';
1372   elsif (lower_cs = 'koi8-u') then   ht_charset := 'CL8KOI8U';
1373   else
1374     ht_charset := db_charset; -- unknown
1375   end if;
1376   last_iana_charset := lower_cs; -- cache it.
1377   ht_charset_ID := NLS_CHARSET_ID(ht_charset); -- Get ID for fast comparison
1378   got_ht_charset := TRUE;
1379 end setHTTPCharset;
1380 
1381 procedure putraw(bbuf in raw, buflen pls_integer DEFAULT null) is
1382   blen pls_integer;
1383   bloc pls_integer;
1384   bcpy pls_integer;
1385 begin
1386   if (bbuf is not null) then
1387     if (buflen is not null) then
1388        blen := buflen;
1389     else
1390        blen := UTL_RAW.LENGTH(bbuf);
1391     end if;
1392     -- Transfer the contents to the raw array
1393     -- Not to pack the buffer since packing a RAW buffer is slow.
1394     contentLen := contentLen + blen;
1395     if (blen <= RAW_MAX) then
1396       rows_in := rows_in + 1;
1397       htraws(rows_in) := bbuf;
1398       htbuf(rows_in) := '';
1399       return;
1400     end if;
1401 
1402     bloc := 1;
1403     while (bloc <= blen) loop
1404       rows_in := rows_in + 1;
1405       bcpy := least((blen - bloc) + 1, RAW_MAX);
1406       htraws(rows_in) := UTL_RAW.SUBSTR(bbuf,bloc,bcpy);
1407       htbuf(rows_in) := '';
1408       bloc := bloc + bcpy;
1409     end loop;
1410   end if;
1411 end putraw;
1412 
1413 procedure per_request_init is
1414    cversion  varchar2(40);
1415 begin
1416    if (nGatewayVersion = 0) then
1417       /* Get the gateway version and cache it for this request */
1418       cversion := owa_util.get_cgi_env ('GATEWAY_IVERSION');
1419       if (cversion is not null)
1420       then
1421          begin
1422             nGatewayVersion := to_number (cversion);
1423 
1424             -- Reserve space for owa_cache headers
1425             if ((nGatewayVersion >= 2) AND (rows_in = 0))
1426             then
1427                owa_cache.init(htbuf, rows_in);
1428             end if;
1429          exception
1430             when VALUE_ERROR then
1431                null;
1432          end;
1433       end if;
1434    end if;
1435 end per_request_init;
1436 
1437 procedure check_request_charset is
1438   nIx      pls_integer;
1439   loc      pls_integer;
1440   ccharset VARCHAR2(256);
1441   bbuf     varchar2(2000);
1442 begin
1443   -- Scan for Content-Type and get character set to compare with DB
1444   for nIx in 1..nEndOfHdrIx loop
1445     if (nIx <> nContentLengthIx) then
1446       bbuf := htbuf(nIx);
1447       if (length(bbuf) > nContentTypeLen) and
1448          (upper(substr(bbuf, 1, nContentTypeLen)) = sContentType) then
1449         loc := instr(bbuf, 'charset=');
1450         if (loc > 0) then
1451           ccharset := substr(bbuf, loc + 8);
1452           loc := instr(ccharset, NL_CHAR);
1453           if (loc > 0) then
1454             ccharset := substr(ccharset, 1, loc - 1);
1455           end if;
1456           setHTTPCharset(ccharset);
1457         end if;
1458         exit;
1459       end if;
1460     end if;
1461   end loop;
1462   if (NOT got_ht_charset) then
1463     setHTTPCharset(null); -- force it to have some value
1464   end if;
1468    loc          pls_integer;
1465 end check_request_charset;
1466 
1467 procedure prn (cbuf in varchar2 character set any_cs DEFAULT NULL) is
1469    len          pls_integer;
1470    len4         pls_integer;
1471    tlen         pls_integer;
1472    ccharset     varchar2(40);
1473    bHasHTMLHdr  boolean;
1474    bUseSubstr   boolean := true; -- use simple substr() function
1475 begin
1476    if (cbuf is NULL)
1477    then
1478       return;
1479    end if;
1480 
1481    if (bRawMode) then
1482        prn_raw(cbuf);
1483        return;
1484    end if;
1485 
1486    --
1487    -- We used to have a separate prn_char procedure for CHAR mode processing,
1488    -- analogous to prn_raw. We now embed the code directly in prn in order
1489    -- to avoid the extra fn call overhead.
1490    --
1491    if (bFirstCall)
1492    then
1493       bFirstCall := FALSE;
1494       per_request_init;
1495 
1496       if (bAddDefaultHTMLHdr)
1497       then
1498          bHTMLPageReady := FALSE;
1499          bHasContentLength := FALSE;
1500          nEndOfHdrIx := -1;
1501          nContentLengthIx := -1;
1502          -- Check for HTML headers
1503          bHasHTMLHdr := isHTMLHdr(upper(cbuf));
1504          if (not bHasHTMLHdr)
1505          then
1506             -- add Content-type: text/html[; charset=<IANA_CHARSET_NAME> ]
1507             rows_in := rows_in + 1;
1508             ccharset := owa_util.get_cgi_env('REQUEST_IANA_CHARSET');
1509             if (ccharset is null) then
1510                htbuf(rows_in) := 'Content-type: ' || stexthtml || NL_CHAR;
1511             else
1512                htbuf(rows_in) := 'Content-type: ' || sTextHtml || '; charset='
1513                       || ccharset || NL_CHAR;
1514             end if;
1515 
1516             -- reserve space for Content-length: header
1517             rows_in := rows_in + 1;
1518             nContentLengthIx := rows_in;
1519             rows_in := rows_in + 1;
1520             htbuf(rows_in) := NL_CHAR;
1521             nEndOfHdrIx := rows_in;
1522             if (nGatewayVersion > 2) then
1523                pack_after := nEndOfHdrIx;
1524             end if;
1525             bHasContentLength := TRUE;
1526          end if;
1527       else
1528          bHTMLPageReady := TRUE;
1529       end if;
1530    end if;
1531 
1532    len := length(cbuf);
1533    len4 := length4(cbuf);
1534    if len <> len4 then
1535       bUseSubstr := false;
1536    end if;
1537 
1538    if (not bHTMLPageReady)
1539    then
1540       -- We assume that 'pack_after' is sufficiently large that we won't be
1541       -- packing HTML headers.
1542       -- We also assume that end of headers request will be by itself
1543       if (nEndOfHdrIx < 0) -- we have not seen end of headers yet
1544       then
1545          if ((len >= nContentLengthLen) and
1546              (sContentLength = substr(upper(cbuf), 1, nContentLengthLen)))
1547          then
1548             bHasContentLength := TRUE;
1549          end if;
1550          if ((cbuf = NL_CHAR
1551                  and (rows_in > 0
1552                          and substr(htbuf(rows_in), length(htbuf(rows_in)), 1)
1553                                 = NL_CHAR)
1554              ) or (instr(cbuf, NLNL_CHAR, -1) != 0))
1555          then -- we now have seen!
1556             if (not bHasContentLength)
1557             then
1558                -- reserve space for Content-length: header
1559                rows_in := rows_in + 1;
1560                nContentLengthIx := rows_in;
1561             end if;
1562             nEndOfHdrIx := (rows_in + 1);
1563             if (nGatewayVersion > 2) then
1564                pack_after := nEndOfHdrIx;
1565             end if;
1566             bHasContentLength := TRUE;
1567             --
1568             --    cbuf should be inserted into the header buffer here.
1569             --
1570             rows_in := rows_in + 1;
1571             htbuf(rows_in) := cbuf;
1572             return;
1573          end if;
1574       end if;
1575    end if;
1576 
1577    if not bUseSubstr then
1578       len := len4;
1579    end if;
1580 
1581    loc := 0;
1582    if (rows_in < pack_after) then
1583       while ((len - loc) >= HTBUF_LEN)
1584       loop
1585          rows_in := rows_in + 1;
1586          if bUseSubstr then
1587              htbuf(rows_in) := substr(cbuf, loc + 1, HTBUF_LEN);
1588          else
1589              htbuf(rows_in) := substr4(cbuf, loc + 1, HTBUF_LEN);
1590          end if;
1591          loc := loc + HTBUF_LEN;
1592       end loop;
1593       if (loc < len)
1594       then
1595          rows_in := rows_in + 1;
1596          if bUseSubstr then
1597              htbuf(rows_in) := substr(cbuf, loc + 1);
1598          else
1599              htbuf(rows_in) := substr4(cbuf, loc + 1);
1600          end if;
1601       end if;
1602       return;
1603    end if;
1604 
1605    if (htcurline is null)
1606    then
1607       tlen := HTBUF_LEN;
1608    else
1609       if bUseSubstr then
1610           tlen := HTBUF_LEN - length(htcurline);
1611       else
1612           tlen := HTBUF_LEN - length4(htcurline);
1613       end if;
1614    end if;
1615 
1616    while (loc < len)
1617    loop
1618       if ((len - loc) <= tlen)
1619       then
1623          else
1620          if (loc = 0)
1621          then
1622             htcurline := htcurline || cbuf;
1624             if bUseSubstr then
1625                htcurline := htcurline || substr(cbuf, loc + 1);
1626             else
1627                htcurline := htcurline || substr4(cbuf, loc + 1);
1628             end if;
1629          end if;
1630          exit;
1631       end if;
1632       rows_in := rows_in + 1;
1633       if bUseSubstr then
1634          htbuf(rows_in) := htcurline || substr(cbuf, loc + 1, tlen);
1635       else
1636          htbuf(rows_in) := htcurline || substr4(cbuf, loc + 1, tlen);
1637       end if;
1638       htcurline := '';
1639       loc := loc + tlen;
1640       tlen := HTBUF_LEN; -- remaining buffer size
1641    end loop;
1642 end prn;
1643 
1644 procedure prn_raw(cbuf in varchar2 character set any_cs) is
1645    loc          pls_integer;
1646    len          pls_integer;
1647    ccharset     varchar2(40);
1648    bHasHTMLHdr  boolean;
1649 begin
1650    if (bFirstCall)
1651    then
1652       bFirstCall := FALSE;
1653 
1654       /*
1655       **   Get the NCHAR character set for later use by CONVERT.
1656       **    Unfortunately it's only available from a view, so we
1657       **    have to run SQL to get it.  Put it here rather than in
1658       **    init so that hopefully it only gets run once per
1659       **    database session.
1660       */
1661       select VALUE into nc_charset from V$NLS_PARAMETERS
1662           where PARAMETER = 'NLS_NCHAR_CHARACTERSET';
1663       nc_charset_ID := NLS_CHARSET_ID (nc_charset);
1664 
1665       -- Initialize ID here when we are in RAW mode
1666       db_charset_ID := NLS_CHARSET_ID(db_charset);
1667       ht_charset_ID := db_charset_ID;
1668 
1669       per_request_init;
1670 
1671       if (bAddDefaultHTMLHdr)
1672       then
1673          bHTMLPageReady := FALSE;
1674          bHasContentLength := FALSE;
1675          nEndOfHdrIx := -1;
1676          nContentLengthIx := -1;
1677          -- Check for HTML headers
1678          bHasHTMLHdr := isHTMLHdr(upper(cbuf));
1679          if (not bHasHTMLHdr)
1680          then
1681             -- add Content-type: text/html[; charset=<IANA_CHARSET_NAME> ]
1682             rows_in := rows_in + 1;
1683             ccharset := owa_util.get_cgi_env('REQUEST_IANA_CHARSET');
1684             if (ccharset is null) then
1685                htbuf(rows_in) := 'Content-type: ' || stexthtml || NL_CHAR;
1686             else
1687                htbuf(rows_in) := 'Content-type: ' || sTextHtml || '; charset='
1688                                   || ccharset || NL_CHAR;
1689             end if;
1690             setHTTPCharset(ccharset, owa_util.get_cgi_env('REQUEST_CHARSET'));
1691 
1692             -- reserve space for Content-length: header
1693             rows_in := rows_in + 1;
1694             nContentLengthIx := rows_in;
1695             rows_in := rows_in + 1;
1696             htbuf(rows_in) := NL_CHAR;
1697             nEndOfHdrIx := rows_in;
1698             if (nGatewayVersion > 2) then
1699                pack_after := nEndOfHdrIx;
1700             end if;
1701             bHasContentLength := TRUE;
1702          end if;
1703       else
1704          bHTMLPageReady := TRUE;
1705       end if;
1706    end if;
1707 
1708    len := lengthb(cbuf);
1709    if (not bHTMLPageReady)
1710    then
1711       -- We assume that 'pack_after' is sufficiently large that we won't be
1712       -- packing HTML headers.
1713       -- We also assume that end of headers request will be by itself
1714       if (nEndOfHdrIx < 0) -- we have not seen end of headers yet
1715       then
1716          if ((len >= nContentLengthLen) and
1717              (sContentLength = substr(upper(cbuf), 1, nContentLengthLen)))
1718          then
1719             bHasContentLength := TRUE;
1720          end if;
1721          if ((cbuf = NL_CHAR
1722                  and (rows_in > 0
1723                          and substr(htbuf(rows_in), lengthb(htbuf(rows_in)), 1)
1724                                 = NL_CHAR)
1725              ) or (instr(cbuf, NLNL_CHAR, -1) != 0))
1726          then -- we now have seen!
1727             if (not bHasContentLength)
1728             then
1729                -- reserve space for Content-length: header
1730                rows_in := rows_in + 1;
1731                nContentLengthIx := rows_in;
1732             end if;
1733             nEndOfHdrIx := rows_in + 1;
1734             if (nGatewayVersion > 2) then
1735                pack_after := nEndOfHdrIx;
1736             end if;
1737             bHasContentLength := TRUE;
1738             --
1739             --    cbuf should be inserted into the header buffer here.
1740             --
1741             rows_in := rows_in + 1;
1742             htbuf(rows_in) := cbuf;
1743             return;
1744          end if;
1745       else
1746          if (not got_ht_charset) then
1747             check_request_charset;
1748          end if;
1749       end if;
1750    end if;
1751 
1752    if (nEndOfHdrIx >= 0) or (rows_in >= pack_after) then
1753       if (isnchar(cbuf))
1754       then
1755          if (nc_charset_ID <> ht_charset_ID) then
1756             putraw(UTL_RAW.CONVERT(UTL_RAW.CAST_TO_RAW(cbuf),
1757                            'AMERICAN_AMERICA.'||ht_charset,
1758                            'AMERICAN_AMERICA.'||nc_charset));
1759          else
1760             if (len > RAW_MAX) then
1761               putraw(UTL_RAW.CAST_TO_RAW(cbuf), len);
1762             else
1763               rows_in := rows_in + 1;
1764               htraws(rows_in) := UTL_RAW.CAST_TO_RAW(cbuf);
1765               htbuf(rows_in) := '';
1766               contentlen := contentLen + len;
1767             end if;
1768          end if;
1769       else
1770          if (db_charset_ID <> ht_charset_ID) then
1771             putraw(UTL_RAW.CONVERT(UTL_RAW.CAST_TO_RAW(cbuf),
1772                        'AMERICAN_AMERICA.'||ht_charset,
1773                        'AMERICAN_AMERICA.'||db_charset));
1774          else
1775             if (len > RAW_MAX) then
1776               putraw(UTL_RAW.CAST_TO_RAW(cbuf),len);
1777             else
1778               rows_in := rows_in + 1;
1779               htraws(rows_in) := UTL_RAW.CAST_TO_RAW(cbuf);
1780               htbuf(rows_in) := '';
1781               contentlen := contentLen + len;
1782             end if;
1783          end if;
1784      end if;
1785      return;
1786    end if;
1787 
1788    len := length(cbuf);
1789    loc := 0;
1790    while ((len - loc) >= HTBUF_LEN)
1791    loop
1792       rows_in := rows_in + 1;
1793       htbuf(rows_in) := substr(cbuf, loc + 1, HTBUF_LEN);
1794       loc := loc + HTBUF_LEN;
1795    end loop;
1796    if (loc < len)
1797    then
1798       rows_in := rows_in + 1;
1799       htbuf(rows_in) := substr(cbuf, loc + 1);
1800    end if;
1801 end prn_raw;
1802 
1803 --
1804 -- Code for htp.p and htp.print is duplicated so that we avoid a
1805 -- procedure call overhead in the typical cases
1806 --
1807 procedure print (cbuf in varchar2 character set any_cs DEFAULT NULL) is
1808 begin
1809     prn(cbuf || NL_CHAR);
1810 end;
1811 
1812 procedure p (cbuf in varchar2 character set any_cs DEFAULT NULL) is
1813 begin
1814     prn(cbuf || NL_CHAR);
1815 end;
1816 
1817 procedure print (dbuf in date) is
1818 begin print(to_char(dbuf)); end;
1819 
1820 procedure print (nbuf in number) is
1821 begin print(to_char(nbuf)); end;
1822 
1823 procedure prn (dbuf in date) is
1824 begin prn(to_char(dbuf)); end;
1825 
1826 procedure prn (nbuf in number) is
1827 begin prn(to_char(nbuf)); end;
1828 
1829 procedure p (dbuf in date) is
1830 begin print(to_char(dbuf)); end;
1831 
1832 procedure p (nbuf in number) is
1833 begin print(to_char(nbuf)); end;
1834 
1835 procedure prints(ctext in varchar2 character set any_cs) is
1836 begin p(htf.escape_sc(ctext)); end;
1837 
1838 procedure ps(ctext in varchar2 character set any_cs) is
1839 begin p(htf.escape_sc(ctext)); end;
1840 
1841 procedure escape_sc(ctext in varchar2 character set any_cs) is
1842 begin p(htf.escape_sc(ctext)); end;
1843 
1844 procedure print_header (cbuf in varchar2, nline in number) is
1845 begin
1846    per_request_init;
1847    if (nGatewayVersion >= 2)
1848    then
1849       htbuf(nline) := cbuf || NL_CHAR;
1850    end if;
1851 end;
1852 /* END SPECIAL PROCEDURES */
1853 
1854 begin
1855    init;
1856 end;