~ubuntu-branches/ubuntu/trusty/ocamlnet/trusty

« back to all changes in this revision

Viewing changes to examples/cgi/netcgi1/fcgi/add_fastcgi.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu
  • Date: 2011-09-02 14:12:33 UTC
  • mfrom: (18.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110902141233-zbj0ygxb92u6gy4z
Tags: 3.4-1
* New upstream release
  - add a new NetcgiRequire directive to ease dependency management
    (Closes: #637147)
  - remove patches that were applied upstream:
    + Added-missing-shebang-lines-in-example-shell-scripts
    + Try-also-ocamlc-for-POSIX-threads

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(* $Id: add_fastcgi.ml 1004 2006-09-25 16:01:06Z gerd $
2
 
 * ----------------------------------------------------------------------
3
 
 *
4
 
 *)
5
 
 
6
 
open Netcgi;;
7
 
open Netcgi_types;;
8
 
open Netcgi_fcgi;;
9
 
open Printf;;
10
 
 
11
 
(***********************************************************************
12
 
 * This is the adder for fcgi
13
 
 ***********************************************************************)
14
 
 
15
 
let text = Netencoding.Html.encode_from_latin1;;
16
 
(* This function encodes "<", ">", "&", double quotes, and Latin 1 characters 
17
 
 * as character entities. E.g. text "<" = "&lt;", and text "�" = "&auml;"
18
 
 *)
19
 
 
20
 
let begin_page (cgi: cgi_activation) title =
21
 
  (* Output the beginning of the page with the passed [title]. *)
22
 
  let out = cgi # output # output_string in
23
 
    out "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n";
24
 
    out "<!--\n";
25
 
    (List.iter (* dump the header fields *)
26
 
       (fun (name, valu) -> out (name ^ ": " ^ valu ^ "\n"))
27
 
       cgi#environment#input_header_fields);
28
 
    out "cookies\n--------\n";
29
 
    (List.iter (* dump cookies *)
30
 
       (fun (name, valu) -> out (name ^ ": " ^ valu ^ "\n"))
31
 
       cgi#environment#cookies);
32
 
    out ("remote host: " ^ (cgi#environment#cgi_remote_addr) ^ "\n");
33
 
    out "-->\n";
34
 
    out "<HTML>\n";
35
 
    out "<HEAD>\n";
36
 
    out ("<TITLE>" ^ text title ^ "</TITLE>\n");
37
 
    out ("<STYLE TYPE=\"text/css\">\n");
38
 
    out "body { background: white; color: black; }\n";
39
 
    out "</STYLE>\n";
40
 
    out "</HEAD>\n";
41
 
    out "<BODY>\n";
42
 
    (* So we can find out the PID: *)
43
 
    out ("<!-- PID: " ^ string_of_int (Unix.getpid()) ^ " -->\n");
44
 
    out ("<H1>" ^ text title ^ "</H1>\n")
45
 
;;
46
 
 
47
 
 
48
 
let end_page cgi =
49
 
  let out = cgi # output # output_string in
50
 
  out "</BODY>\n";
51
 
  out "</HTML>\n"
52
 
;;
53
 
 
54
 
 
55
 
let generate_query_page (cgi : cgi_activation) =
56
 
  (* Display the query form. *)
57
 
  begin_page cgi "Add Two Numbers";
58
 
  let out = cgi # output # output_string in
59
 
  out "<P>This CGI page can perform additions. Please enter two integers,\n";
60
 
  out "and press the button!\n";
61
 
  out (sprintf "<P><FORM METHOD=POST ACTION=\"%s\">\n" 
62
 
         (text (cgi#url())));
63
 
  (* Note that cgi#url() returns the URL of this script (without ? clause).
64
 
   * We pass this string through the text function to avoid problems with
65
 
   * some characters.
66
 
   *)
67
 
  out "<INPUT TYPE=TEXT NAME=\"x\"> + <INPUT TYPE=TEXT NAME=\"y\"> = ";
68
 
  out "<INPUT TYPE=SUBMIT NAME=\"button\" VALUE=\"Go!\">\n";
69
 
  (* The hidden field only indicates that now the result page should
70
 
   * be consulted.
71
 
   *)
72
 
  out "<INPUT TYPE=HIDDEN NAME=\"page\" VALUE=\"result\">\n";
73
 
  out "</FORM>\n";
74
 
  end_page cgi
75
 
;;
76
 
 
77
 
 
78
 
let generate_result_page (cgi : cgi_activation) =
79
 
  (* Compute the result, and display it *)
80
 
  begin_page cgi "Sum";
81
 
  let out = cgi # output # output_string in
82
 
  out "<P>The result is:\n";
83
 
  let x = cgi # argument_value "x" in
84
 
  let y = cgi # argument_value "y" in
85
 
  let sum = (int_of_string x) + (int_of_string y) in
86
 
  out (sprintf "<P>%s + %s = %d\n" x y sum);
87
 
  out (sprintf "<P><A HREF=\"%s\">Add further numbers</A>\n" 
88
 
         (text (cgi#url 
89
 
                  ~with_query_string:
90
 
                                   (`Args [new simple_argument "page" "query"])
91
 
                  ()
92
 
               )));
93
 
  (* Here, the URL contains the CGI argument "page", but no other arguments. *)
94
 
  end_page cgi
95
 
;;
96
 
 
97
 
 
98
 
let generate_page (cgi : cgi_activation) =
99
 
  (* Check which page is to be displayed. This is contained in the CGI
100
 
   * argument "page".
101
 
   *)
102
 
  match cgi # argument_value "page" with
103
 
      "" ->
104
 
        (* The argument is the empty string, or the argument is missing.
105
 
         * This is the same like the page "query".
106
 
         *)
107
 
        generate_query_page cgi
108
 
    | "query" ->
109
 
        generate_query_page cgi
110
 
    | "result" ->
111
 
        generate_result_page cgi
112
 
    | _ ->
113
 
        assert false
114
 
;;
115
 
 
116
 
 
117
 
let process (cgi : cgi_activation) =
118
 
  (* The [try] block catches errors during the page generation. *)
119
 
  try
120
 
    (* Set the header. The header specifies that the page must not be
121
 
     * cached. This is important for dynamic pages called by the GET
122
 
     * method, otherwise the browser might display an old version of
123
 
     * the page.
124
 
     * Furthermore, we set the content type and the character set.
125
 
     * Note that the header is not sent immediately to the browser because
126
 
     * we have enabled HTML buffering.
127
 
     *)
128
 
    cgi # set_header 
129
 
      ~cache:`No_cache 
130
 
      ~content_type:"text/html; charset=\"iso-8859-1\""
131
 
      ();
132
 
 
133
 
    generate_page cgi;
134
 
 
135
 
    (* After the page has been fully generated, we can send it to the
136
 
     * browser. 
137
 
     *)
138
 
    cgi # output # commit_work();
139
 
  with
140
 
      error ->
141
 
        (* An error has happened. Generate now an error page instead of
142
 
         * the current page. By rolling back the output buffer, any 
143
 
         * uncomitted material is deleted.
144
 
         *)
145
 
        cgi # output # rollback_work();
146
 
 
147
 
        (* We change the header here only to demonstrate that this is
148
 
         * possible.
149
 
         *)
150
 
        cgi # set_header 
151
 
          ~status:`Forbidden                  (* Indicate the error *)
152
 
          ~cache:`No_cache 
153
 
          ~content_type:"text/html; charset=\"iso-8859-1\""
154
 
          ();
155
 
 
156
 
        begin_page cgi "Software error";
157
 
        cgi # output # output_string "While processing the request an O'Caml exception has been raised:<BR>";
158
 
        cgi # output # output_string ("<TT>" ^ text(Printexc.to_string error) ^ "</TT><BR>");
159
 
        end_page cgi;
160
 
 
161
 
        (* Now commit the error page: *)
162
 
        cgi # output # commit_work()
163
 
;;
164
 
 
165
 
(* start the fastcgi server *)
166
 
serv process buffered_transactional_optype;;
167
 
  
168
 
 
169
 
(* ======================================================================
170
 
 * History:
171
 
 * 
172
 
 * $Log$
173
 
 * Revision 1.3  2004/11/24 18:01:07  gremlin43820
174
 
 * commiting fastcgi compatibility patch from Kristof Pap which allows us to talk to a wider variety of fastcgi enabled web servers, and more closely follows the standard.
175
 
 *
176
 
 * Revision 1.1  2003/10/07 17:39:32  stolpmann
177
 
 *      Imported Eric's patch for fastcgi
178
 
 *
179
 
 * Revision 1.1  2003/10/03 22:02:07  eric
180
 
 * added an example for fastcgi
181
 
 *
182
 
 * Revision 1.1  2002/03/25 00:12:50  stolpmann
183
 
 *      Initial revision
184
 
 *
185
 
 * Revision 1.1  2002/02/03 21:31:34  stolpmann
186
 
 *      Initial revision.
187
 
 *
188
 
 *)