Commit 07f1bbce4bbd814d97625f59577e98fc4f8eceea

Authored by totoro
1 parent c65648e5

add helper for img_button with only img and web_action

add AWP_Handler_Answer instead of printable tree
add js_script which is helper for js_inline(script("text/javascript"), "...
web/CXM_making_a_web_site.anubis
... ... @@ -350,7 +350,10 @@ public type WEB_Controller_Result:
350 350 WEB_Session session, //modified session
351 351 Printable_tree content,
352 352 String additional_script
353   - )
  353 + ),
  354 + send_file(
  355 + String full_path //full path with file name of the file to send
  356 + )
354 357 .
355 358  
356 359 public define WEB_Controller_Result
... ... @@ -1277,7 +1280,7 @@ public type JS_File:
1277 1280 public type Script:
1278 1281 script( String script_type,
1279 1282 String content).
1280   -
  1283 +
1281 1284 public define JS_File
1282 1285 js_file
1283 1286 (
... ... @@ -1298,6 +1301,13 @@ public type HTML_Head_Tag:
1298 1301 css_inline(String css_styles) /* Note: <style> and </style> tags are not necessaries */
1299 1302 .
1300 1303  
  1304 +public define HTML_Head_Tag
  1305 + js_script
  1306 + (
  1307 + String script_content
  1308 + )=
  1309 + js_inline(script("text/javascript", script_content))
  1310 +.
1301 1311 /* Label with help */
1302 1312  
1303 1313  
... ... @@ -1733,17 +1743,22 @@ public type HTML_Off_Form:
1733 1743 button (List(CoreAttrs), List(HTML_Off_Form) content),
1734 1744 i (List(CoreAttrs), String text),
1735 1745 span (List(CoreAttrs), String text),
  1746 + strong (List(CoreAttrs), List(HTML_Off_Form) content),
1736 1747 hr (List(CoreAttrs)),
1737 1748 h1 (List(CoreAttrs), List(HTML_Off_Form) content),
1738 1749 h2 (List(CoreAttrs), List(HTML_Off_Form) content),
1739 1750 h3 (List(CoreAttrs), List(HTML_Off_Form) content),
1740 1751 h4 (List(CoreAttrs), List(HTML_Off_Form) content),
1741 1752 h5 (List(CoreAttrs), List(HTML_Off_Form) content),
1742   - h6 (List(CoreAttrs), List(HTML_Off_Form) content)
  1753 + h6 (List(CoreAttrs), List(HTML_Off_Form) content)
1743 1754 .
1744 1755  
1745 1756 'HTML_Off_Form' defines all the elements you may put outside any form.
1746 1757  
  1758 +public define HTML_Off_Form strong(HTML_Off_Form content) = strong([], [content]).
  1759 +public define HTML_Off_Form strong(List(HTML_Off_Form) content) = strong([], content).
  1760 +public define HTML_Off_Form strong(List(CoreAttrs) attrs, HTML_Off_Form content) = strong(attrs, [content]).
  1761 +
1747 1762 public define HTML_Off_Form h1(HTML_Off_Form content) = h1([], [content]).
1748 1763 public define HTML_Off_Form h1(List(HTML_Off_Form) content) = h1([], content).
1749 1764 public define HTML_Off_Form h1(List(CoreAttrs) attrs, HTML_Off_Form content) = h1(attrs, [content]).
... ... @@ -2434,6 +2449,7 @@ public type HTTP_Answer:
2434 2449 Printable_tree /*content*/),
2435 2450 json (HTTP_Status, /*http_status*/
2436 2451 JsonValue), /*json value*/
  2452 + send_file (String file_path), //file to send to client. //TODO add call back for before, during, after send
2437 2453 http_raw (Printable_tree),
2438 2454 html_content (HTTP_Status /*http_status*/,
2439 2455 HTML_Off_Form),
... ... @@ -3160,16 +3176,15 @@ public type CommonInfo:
3160 3176  
3161 3177 We need a forward declaration.
3162 3178  
3163   -public define Printable_tree
3164   - format
3165   - (
3166   - CommonInfo cinfo,
3167   - //String state_name,
3168   - List(HTTP_header) headers,
3169   - HTTP_Answer page,
3170   - Bool is_https,
3171   - String charset
3172   - ).
  3179 +public define AWP_Handler_Answer
  3180 + format
  3181 + (
  3182 + CommonInfo cinfo,
  3183 + List(HTTP_header) headers,
  3184 + HTTP_Answer page,
  3185 + Bool is_https,
  3186 + String charset
  3187 + ).
3173 3188  
3174 3189  
3175 3190 define Printable_tree
... ... @@ -3218,7 +3233,7 @@ public define Web_Site
3218 3233 List(Web_arg),
3219 3234 Bool is_https) -> $State ticket_lost_state,
3220 3235 List(Web_Action($State)) actions,
3221   - $State -> HTTP_Answer compute_page,
  3236 + $State -> HTTP_Answer compute_page,
3222 3237 $State -> List(HTTP_header) additional_headers,
3223 3238 Int timeout,
3224 3239 Redirections redirections,
... ... @@ -3256,7 +3271,6 @@ public define Web_Site
3256 3271 HTTP_Info http_info,
3257 3272 List(Web_arg) lwa,
3258 3273 Bool is_https) |->
3259   - //(Printable_tree)
3260 3274 //println(dump_web_arg_values(lwa));
3261 3275 since separate_web_args(lwa, http_info) is swa(mb_previous_state, mb_action_name, operands),
3262 3276 with new_state = if mb_previous_state is
... ... @@ -3300,7 +3314,6 @@ public define Web_Site
3300 3314 },
3301 3315  
3302 3316 format(info(host_name, http_port, https_port, site_directory, secret),
3303   - //state_name,
3304 3317 additional_headers(new_state) + cookie_headers,
3305 3318 compute_page(new_state),
3306 3319 is_https,
... ... @@ -3324,8 +3337,8 @@ public define Web_Site
3324 3337 journal_headers,
3325 3338 secret,
3326 3339 known_mime_types,
3327   - site_handler(http_port,https_port),
3328   - (HTTP_Info http_info, List(Web_arg) lwa) |-> if separate_web_args(lwa, http_info ) is
  3340 + site_handler(http_port, https_port),
  3341 + (HTTP_Info http_info, List(Web_arg) lwa) |-> if separate_web_args(lwa, http_info ) is
3329 3342 swa(mb_previous_state,mb_action_name,operands) then
3330 3343 if mb_action_name is
3331 3344 {
... ... @@ -3539,7 +3552,7 @@ define (Maybe(WEB_Session), HTTP_Answer)
3539 3552 ajax(session, content, additional_script) then (success(session), json(http_ok, to_html_ajax_content(content, cinfo, additional_script))),
3540 3553 ajax(content, additional_script) then (failure, json(http_ok, to_html_ajax_content(content, additional_script))),
3541 3554 ajax(session, content, additional_script) then (success(session), json(http_ok, to_html_ajax_content(content, additional_script))),
3542   -
  3555 + send_file(file_path) then (failure, send_file(file_path))
3543 3556 }
3544 3557 }
3545 3558 .
... ... @@ -5810,6 +5823,8 @@ define Printable_tree
5810 5823 format(cinfo,ic_v,any_i(opts,t),format_element,is_https, action_count, head_tags),
5811 5824 span(opts,t) then
5812 5825 format(cinfo,ic_v,any_span(opts,t),format_element,is_https, action_count, head_tags),
  5826 + strong(opts, l) then ["<strong", format_attrs(opts), ">",flat(map(format_element,l)),"</strong>\n"],
  5827 +
5813 5828 hr(opts) then
5814 5829 format(cinfo,ic_v,any_hr(opts),format_element,is_https, action_count, head_tags),
5815 5830 h1(opts, l) then ["<h1", format_attrs(opts), ">",flat(map(format_element,l)),"</h1>\n"],
... ... @@ -6054,19 +6069,18 @@ public define JsonValue
6054 6069 ])
6055 6070 .
6056 6071  
6057   -public define Printable_tree
6058   - format
6059   - (
6060   - CommonInfo cinfo,
6061   - //String state_name,
6062   - List(HTTP_header) additional_headers,
6063   - HTTP_Answer page,
6064   - Bool is_https,
6065   - String charset
6066   - ) =
6067   - if cinfo is info(common_name,http_port,https_port,site_directory,secret) then
6068   - with ic_v = var((Int)0),
6069   - p_content_head_tags = var((List(HTML_Head_Tag))[]), //list of heat_tags extracted from partial_content during format of body content
  6072 +public define AWP_Handler_Answer //Printable_tree
  6073 + format
  6074 + (
  6075 + CommonInfo cinfo,
  6076 + List(HTTP_header) additional_headers,
  6077 + HTTP_Answer page,
  6078 + Bool is_https,
  6079 + String charset
  6080 + )=
  6081 + if cinfo is info(common_name,http_port,https_port,site_directory,secret) then
  6082 + with ic_v = var((Int)0),
  6083 + p_content_head_tags = var((List(HTML_Head_Tag))[]), //list of heat_tags extracted from partial_content during format of body content
6070 6084 if page is
6071 6085 {
6072 6086 html_page(status, head_tags, body) then
... ... @@ -6085,15 +6099,16 @@ public define Printable_tree
6085 6099  
6086 6100 "<head>\n", format_html_head(head_tags + *p_content_head_tags, charset),"</head>\n",
6087 6101 ],
6088   - [ "HTTP/1.1 " + status_string, crlf,
6089   - format_headers(standard_headers),
  6102 + printable_tree([
  6103 + "HTTP/1.1 " + status_string, crlf,
  6104 + format_headers(standard_headers),
6090 6105 format_headers(standard_headers_for("text/html", length(answer_body_header) + length(answer_body_body), success(charset))),
6091 6106 format_headers(status_headers),
6092 6107 format_headers(additional_headers),
6093 6108 crlf,
6094 6109 answer_body_header .
6095 6110 answer_body_body
6096   - ],
  6111 + ]),
6097 6112  
6098 6113 html_page(status, title, metas, css_styles, css_files, js_files, script, body) then
6099 6114 if body is body(options,element) then
... ... @@ -6130,41 +6145,45 @@ public define Printable_tree
6130 6145 format_html_head(*p_content_head_tags, charset),
6131 6146 "</head>\n"
6132 6147 ],
6133   -
6134   - [ "HTTP/1.1 " + status_string, crlf,
6135   - format_headers(standard_headers),
6136   - format_headers(standard_headers_for("text/html", length(answer_body_header) + length(answer_body_body), success(charset))),
6137   - format_headers(status_headers),
6138   - format_headers(additional_headers),
6139   - crlf,
  6148 + printable_tree([
  6149 + "HTTP/1.1 " + status_string, crlf,
  6150 + format_headers(standard_headers),
  6151 + format_headers(standard_headers_for("text/html", length(answer_body_header) + length(answer_body_body), success(charset))),
  6152 + format_headers(status_headers),
  6153 + format_headers(additional_headers),
  6154 + crlf,
6140 6155 answer_body_header .
6141 6156 answer_body_body
6142   - ],
  6157 + ]),
6143 6158  
6144 6159 plain_text (HTTP_Status status, String text) then
6145 6160 if format(status) is (status_string, status_headers) then
6146   - [ "HTTP/1.1 " + status_string, crlf,
  6161 + printable_tree([
  6162 + "HTTP/1.1 " + status_string, crlf,
6147 6163 format_headers(standard_headers),
6148 6164 format_headers(standard_headers_for("text/plain", length(text), success(charset))),
6149 6165 format_headers(status_headers),
6150 6166 format_headers(additional_headers),
6151 6167 crlf,
6152 6168 text . (Printable_tree)[]
6153   - ],
  6169 + ]),
6154 6170  
6155 6171 custom_text(HTTP_Status status, mime_type, String content) then
6156 6172 if format(status) is (status_string, status_headers) then
6157   - [ "HTTP/1.1 " + status_string, crlf,
  6173 + printable_tree([
  6174 + "HTTP/1.1 " + status_string, crlf,
6158 6175 format_headers(standard_headers),
6159 6176 format_headers(standard_headers_for(to_String(mime_type), length(content), success(charset))),
6160 6177 format_headers(status_headers),
6161 6178 format_headers(additional_headers),
6162 6179 crlf,
6163 6180 content . (Printable_tree)[]
6164   - ],
  6181 + ]),
  6182 +
6165 6183 custom_binary(HTTP_Status status, mime_type, ByteArray content, List(HTTP_header) other_headers) then
6166 6184 if format(status) is (status_string, status_headers) then
6167   - [ "HTTP/1.1 " + status_string, crlf,
  6185 + printable_tree([
  6186 + "HTTP/1.1 " + status_string, crlf,
6168 6187 format_headers(status_headers),
6169 6188 format_headers(standard_headers_for(to_String(mime_type), length(content), failure)),
6170 6189 format_headers(status_headers),
... ... @@ -6172,44 +6191,53 @@ public define Printable_tree
6172 6191 format_headers(additional_headers),
6173 6192 crlf,
6174 6193 content . (Printable_tree)[]
6175   - ],
  6194 + ]),
6176 6195  
6177 6196 custom_tree(HTTP_Status status, mime_type, Printable_tree content) then
6178   - if format(status) is (status_string, status_headers) then
6179   - [ "HTTP/1.1 " + status_string, crlf,
  6197 + if format(status) is (status_string, status_headers) then
  6198 + printable_tree([
  6199 + "HTTP/1.1 " + status_string, crlf,
6180 6200 format_headers(standard_headers),
6181 6201 format_headers(standard_headers_for(to_String(mime_type), length(content), success(charset))),
6182 6202 format_headers(status_headers),
6183 6203 format_headers(additional_headers),
6184 6204 crlf .
6185 6205 content
6186   - ],
  6206 + ]),
  6207 +
6187 6208 json(HTTP_Status status, JsonValue json_content) then
6188 6209 //with start = unow,
6189 6210 with content = (Printable_tree)format_json(json_content),
6190 6211 //println(show_duration_string("HTTP json format json object ", start));
6191 6212 if format(status) is (status_string, status_headers) then
6192   - [ "HTTP/1.1 " + status_string, crlf,
6193   - format_headers(standard_headers),
  6213 + printable_tree([
  6214 + "HTTP/1.1 " + status_string, crlf,
  6215 + format_headers(standard_headers),
6194 6216 format_headers(standard_headers_for(to_String(mime_json), length(content), success(charset))),
6195 6217 format_headers(status_headers),
6196 6218 format_headers(additional_headers),
6197 6219 crlf .
6198 6220 content
6199   - ],
6200   - http_raw(Printable_tree content) then content,
  6221 + ])
  6222 + ,
  6223 + send_file(file_path) then
  6224 + send_file(file_path),
  6225 +
  6226 + http_raw(Printable_tree content) then
  6227 + printable_tree(content),
6201 6228  
6202 6229 html_content(HTTP_Status status, HTML_Off_Form content_HTML) then
6203 6230 if format(status) is (status_string, status_headers) then
6204 6231 with content = format(cinfo, ic_v, content_HTML, is_https, var(0), p_content_head_tags),
6205   - [ "HTTP/1.1 " + status_string, crlf,
  6232 + printable_tree([
  6233 + "HTTP/1.1 " + status_string, crlf,
6206 6234 format_headers(standard_headers),
6207 6235 format_headers(standard_headers_for("text/html", length(content), success(charset))),
6208 6236 format_headers(status_headers),
6209 6237 format_headers(additional_headers),
6210 6238 crlf .
6211 6239 content
6212   - ],
  6240 + ]),
6213 6241 //HMTL content with some header
6214 6242 html_partial_content(HTTP_Status status, HTML_Partial_Content p_content) then
6215 6243  
... ... @@ -6225,7 +6253,8 @@ public define Printable_tree
6225 6253 // html_header,
6226 6254 format_html_head(head_tags + *p_content_head_tags),
6227 6255 ],
6228   - [ "HTTP/1.1 " + status_string, crlf,
  6256 + printable_tree([
  6257 + "HTTP/1.1 " + status_string, crlf,
6229 6258 format_headers(standard_headers),
6230 6259 format_headers(standard_headers_for("text/html", length(answer_header) + length(answer_body), success(charset))),
6231 6260 format_headers(status_headers),
... ... @@ -6233,6 +6262,6 @@ public define Printable_tree
6233 6262 crlf,
6234 6263 answer_header .
6235 6264 answer_body
6236   - ],
  6265 + ]),
6237 6266 }.
6238 6267  
... ...
web/CXM_multihost_http_server.anubis
... ... @@ -188,6 +188,11 @@ read web/mime.anubis
188 188 Each site is described by a 'web site description', which is a datum of type
189 189 'Web_Site_Description'.
190 190  
  191 +public type AWP_Handler_Answer:
  192 + printable_tree(Printable_tree p_tree),
  193 + send_file(String full_path)
  194 +.
  195 +
191 196 public type Web_Site_Description:
192 197 web_site_description(
193 198 List(String) common_names,
... ... @@ -201,8 +206,7 @@ public type Web_Site_Description:
201 206 (String host_name,
202 207 HTTP_Info http_info,
203 208 List(Web_arg) lwa,
204   - Bool is_https) -> (//List(HTTP_header),
205   - Printable_tree) awp_handler,
  209 + Bool is_https) -> AWP_Handler_Answer awp_handler,
206 210 (HTTP_Info http_info,
207 211 List(Web_arg) lwa) -> One before_send_file
208 212 //Bool using_state_cookies,
... ... @@ -2176,11 +2180,14 @@ define One
2176 2180 Int sent, // bytes already sent
2177 2181 String filename // name of file
2178 2182 ) =
2179   - if sent >= size then unique else
  2183 + if sent >= size then
  2184 + //TODO add call back on success if need
  2185 + unique
  2186 + else
2180 2187 if read(file,min(65536,size-sent),60) is
2181 2188 {
2182 2189 error then log_journal_msg(desc,"Cannot read from file '"+filename+"'.\n"),
2183   - timeout then log_journal_msg(desc,"Cannot read from file timeoput'"+filename+"'.\n"),
  2190 + timeout then log_journal_msg(desc,"Cannot read from file timeout'"+filename+"'.\n"),
2184 2191 ok(ba) then
2185 2192 with nr = length(ba), // get the number of bytes read
2186 2193 if reliable_write(connection, ba) is
... ... @@ -2226,17 +2233,18 @@ define One
2226 2233 Connection connection,
2227 2234 List(HTTP_header) input_headers,
2228 2235 List(HTTP_header) headers,
2229   - Int size,
2230   - Connection file,
2231   - String filename,
2232   - String full_path,
2233   - MIME mime_type,
2234   - One -> One action_before_send_file
  2236 + Int size, // size of the file to send
  2237 + Connection file, // file to be sent already opened
  2238 + String filename, // name of file
  2239 + String full_path, // full path to the file (filename included)
  2240 + MIME mime_type, // mime type of the file
  2241 + One -> One action_before_send_file //call to execute before sending file
2235 2242 ) =
2236 2243 action_before_send_file(unique);
2237 2244 with input_etag = http_header_value(input_headers, "If-None-Match"),
2238 2245 mb_ftimes = get_file_times(full_path),
2239   - current_etag = compute_etag(full_path, mb_ftimes, size),
  2246 + current_etag = compute_etag(full_path, mb_ftimes, size),
  2247 +
2240 2248 if are_same_etag(input_etag, current_etag) is
2241 2249 {
2242 2250 false then
... ... @@ -2272,7 +2280,7 @@ define Bool
2272 2280  
2273 2281 Before opening and sending a file, we check the MIME type. It must be recognized,
2274 2282 except if there is a valid authorization for private download.
2275   -
  2283 +
2276 2284 define One
2277 2285 send_file
2278 2286 (
... ... @@ -2350,6 +2358,43 @@ define One
2350 2358 )
2351 2359 else log_journal_msg(desc,"Cannot find or read authorization file.\n")
2352 2360 }.
  2361 +
  2362 +define One
  2363 + send_file_answer
  2364 + (
  2365 + Web_Site_Description desc,
  2366 + Connection connection,
  2367 + String uri,
  2368 + String absolute_path,
  2369 + List(HTTP_header) input_headers,
  2370 + List(HTTP_header) output_headers,
  2371 + One -> One action_before_send_file
  2372 + )=
  2373 + if (Maybe(RStream))file(absolute_path, read) is
  2374 + {
  2375 + failure then
  2376 + println("HTTP/1.1 404 Not Found"+absolute_path);
  2377 + forget(reliable_write(connection,to_byte_array("HTTP/1.1 404 Not Found"+crlf+
  2378 + "Content-Length: 0"+crlf+crlf
  2379 + /*+"Connection: close"+crlf+crlf*/)));
  2380 + log_journal_msg(desc,"Cannot find file '"+absolute_path+"'.\n"),
  2381 + success(f) then
  2382 + with size = file_size(f),
  2383 + mime_type = mime("application", "octet-stream", []),
  2384 +
  2385 + send_file(
  2386 + desc,
  2387 + connection,
  2388 + input_headers,
  2389 + output_headers,
  2390 + size,
  2391 + file(f),
  2392 + uri,
  2393 + absolute_path,
  2394 + mime_type,
  2395 + action_before_send_file)
  2396 + }
  2397 +.
2353 2398  
2354 2399 *** [5.6] Answering a www-url encoded request.
2355 2400  
... ... @@ -2410,14 +2455,15 @@ define One
2410 2455 if is_illegal_uri(uri,0)
2411 2456 then log_journal_msg(desc,"Received illegal URI: "+uri+"\n")
2412 2457 else (if (ext = ".awp" | ext = "")
2413   - then (with answer_headers_body = awp_handler(desc)(host_name,
2414   - http_inf,
2415   - all_web_args,
2416   - is_SSL(connection)),
2417   - //print_delta("After page generation");
2418   - forget(reliable_write(connection, answer_headers_body))
2419   - //print_delta("After sending page")
2420   - )
  2458 + then
  2459 + if awp_handler(desc)(host_name, http_inf, all_web_args, is_SSL(connection)) is
  2460 + {
  2461 + printable_tree(answer_headers_body) then
  2462 + forget(reliable_write(connection, answer_headers_body)),
  2463 + send_file(file_path) then
  2464 + send_file_answer(desc, connection, uri, file_path, headers, standard_headers,
  2465 + (One u) |-> before_send_file(desc)(http_inf, all_web_args))
  2466 + }
2421 2467 else (send_file(desc,
2422 2468 connection,
2423 2469 uri,
... ... @@ -2916,12 +2962,25 @@ define One
2916 2962 if is_illegal_uri(uri,0)
2917 2963 then log_journal_msg(desc,"Received illegal URI: "+uri+"\n")
2918 2964 else
2919   - if (ext = ".awp" | ext = "") then
2920   - (with answer_headers_body = awp_handler(desc)(host_name,
2921   - http_info(ip_addr, host_name, uri, headers, is_SSL(connection)),
2922   - all_web_args,
2923   - is_SSL(connection)),
2924   - forget(reliable_write(connection, answer_headers_body)))
  2965 +// if (ext = ".awp" | ext = "") then
  2966 +// (with answer_headers_body = awp_handler(desc)(host_name,
  2967 +// http_info(ip_addr, host_name, uri, headers, is_SSL(connection)),
  2968 +// all_web_args,
  2969 +// is_SSL(connection)),
  2970 +// forget(reliable_write(connection, answer_headers_body)))
  2971 +
  2972 + if (ext = ".awp" | ext = "") then
  2973 + with http_inf = http_info(ip_addr, host_name, uri, headers, is_SSL(connection)),
  2974 + if awp_handler(desc)(host_name, http_inf, all_web_args, is_SSL(connection)) is
  2975 + {
  2976 + printable_tree(answer_headers_body) then
  2977 + forget(reliable_write(connection, answer_headers_body)),
  2978 + send_file(file_path) then
  2979 + send_file_answer(desc, connection, uri, file_path, headers, standard_headers,
  2980 + (One u) |-> before_send_file(desc)(http_inf, all_web_args))
  2981 + }
  2982 +
  2983 +
2925 2984 else unique
2926 2985 }.
2927 2986  
... ...
web/widgets/button.anubis
... ... @@ -78,10 +78,18 @@ public define HTML_Partial_Content
78 78 img_button
79 79 (
80 80 String img_path,
81   - WEB_Action_Name url,
  81 + WEB_Action_Name web_action,
82 82 List((String, String)) extra_ops
83 83 )=
84   - img_button(img_path, [], same, url, extra_ops).
  84 + img_button(img_path, [], same, web_action, extra_ops).
  85 +
  86 +public define HTML_Partial_Content
  87 + img_button
  88 + (
  89 + String img_path,
  90 + WEB_Action_Name web_action
  91 + )=
  92 + img_button(img_path, [], same, web_action, []).
85 93  
86 94 public define HTML_Partial_Content
87 95 img_button
... ...