~ubuntu-branches/ubuntu/hardy/steam/hardy

« back to all changes in this revision

Viewing changes to server/libraries/httplib.pmod

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2006-11-21 16:03:12 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061121160312-nf96y6nihzsyd2uv
Tags: 2.2.31-3
Add patch to prevent inconsistent data after shutdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 *  along with this program; if not, write to the Free Software
15
15
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
16
 * 
17
 
 * $Id: httplib.pmod,v 1.2 2005/04/27 15:22:53 astra Exp $
 
17
 * $Id: httplib.pmod,v 1.7 2006/08/26 10:23:28 dbuese Exp $
18
18
 */
19
19
 
20
 
constant cvs_version="$Id: httplib.pmod,v 1.2 2005/04/27 15:22:53 astra Exp $";
 
20
constant cvs_version="$Id: httplib.pmod,v 1.7 2006/08/26 10:23:28 dbuese Exp $";
21
21
 
22
22
#include <config.h>
23
23
#include <macros.h>
30
30
                 "Jul":6, "Aug":7, "Sep":8, "Oct":9, "Nov":10, "Dec":11,
31
31
                 "jan":0, "feb":1, "mar":2, "apr":3, "may":4, "jun":5,
32
32
                 "jul":6, "aug":7, "sep":8, "oct":9, "nov":10, "dec":11,]);
33
 
constant montharr = ({ "Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug",
 
33
constant montharr = ({ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
34
34
                           "Sep", "Oct", "Nov", "Dec" });
35
35
 
36
36
 
287
287
                string oname = line[0];
288
288
                int oid;
289
289
                if ( sscanf(oname, "/DB:#%d", oid) > 0 )
290
 
                    oname = get_module("filepath:tree")->
 
290
                    oname = _Server->get_module("filepath:tree")->
291
291
                        object_to_filename(find_object(oid));
292
292
                errStr += sprintf("<li> %s,  line %d</li>\n", oname, line[1]);
293
293
            }
305
305
    return backtrace_html(bt);
306
306
}
307
307
 
308
 
 
309
308
/**
310
309
 * return a page for displaying errors
311
310
 *  
315
314
 */
316
315
string error_page(string message, string|void back)
317
316
{
318
 
    object xsl;
319
 
    string xml;
320
 
    
321
 
    xsl = find_object("/stylesheets/errors.xsl");
322
 
    xml = "<?xml version='1.0' encoding='utf-8'?>"+
323
 
      "<error><actions/>\n<message><![CDATA["+message+
324
 
        "]]></message></error>";
325
 
    return _Server->get_module("libxslt")->run(xml, xsl, (["back": back,]) );
 
317
    string html = "";
 
318
    // need a webinterface version check here
 
319
    object web = _Server->get_module("package:web");
 
320
    string wi_version = "0.0.0";
 
321
    if (objectp(web)) wi_version = web->get_version();
 
322
    array wiv = wi_version / ".";
 
323
    // if webinterface version >= 2.1, then use new method respecting user 
 
324
    // stylesheet setting (and may use internationalization in the future..)
 
325
    if ( (sizeof(wiv) > 0 && (int)wiv[0]>2) || (sizeof(wiv) > 1 && (int)wiv[0]>=2 && (int)wiv[1]>=1) ) {
 
326
      object xsl;
 
327
      string xml;
 
328
 
 
329
      object user = geteuid();
 
330
      if (!objectp(user)) user = this_user();
 
331
      object css_style;
 
332
      if (objectp(user)) css_style = user->query_attribute("USER_CSS_STYLE");
 
333
      xsl = find_object("/stylesheets/errors.xsl");
 
334
      xml = "<?xml version='1.0' encoding='utf-8'?>"+
 
335
        "<Object>";
 
336
      if (objectp(css_style)) {
 
337
        xml += "<properties><css_style><Object><path>"+
 
338
               _Server->get_module("filepath:tree")->object_to_filename(css_style)+
 
339
               "</path></Object></css_style></properties>";
 
340
      }
 
341
      xml += "<actions/>\n<message><![CDATA["+message+
 
342
             "]]></message></Object>";
 
343
 
 
344
      mapping myvars = ([]);
 
345
      myvars["back"] = back;
 
346
      myvars["message"] = message;    
 
347
      /*
 
348
      // doesnt work yet, need help and more testing
 
349
      // produces strange error "Start Tag expected..." but xsl.xml file looks fine ?!
 
350
          xml = _Server->get_module("Converter:XML")->get_xml(this(), xsl, myvars, 0);
 
351
      werror("xml="+xml+"\r\n");
 
352
      */
 
353
      html = _Server->get_module("libxslt")->run(xml, xsl, myvars );
 
354
    }
 
355
    else {  // do it the old-fashioned way...
 
356
      object xsl;
 
357
      string xml;
 
358
      xsl = find_object("/stylesheets/errors.xsl");
 
359
      xml = "<?xml version='1.0' encoding='utf-8'?>"+
 
360
        "<error><actions/>\n<message><![CDATA["+message+
 
361
    "]]></message></error>";
 
362
      html = _Server->get_module("libxslt")->run(xml, xsl, (["back": back,]) );
 
363
    }
 
364
  return html;
326
365
}
327
366
 
328
367
 
339
378
    return "<![CDATA["+replace_uml(str)+"]]>";
340
379
}
341
380
 
 
381
string encode_url(string str)
 
382
{
 
383
    return replace_uml(str);
 
384
}
 
385
 
342
386
string replace_uml(string str)
343
387
{
344
388
  if ( !stringp(str) )
345
389
    return "";
346
 
#if 1
347
 
      return replace(str, 
348
 
                     ({ "�", "�", "�", "�", "�", "�", "�", "<", ">", "?", " ", "#", ":", "\303\244", "\303\266", "\303\274", "\303\234", "\303\204", "\303\226", "\303\237", "\"" }),
349
 
                   ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc", "%df",
350
 
                      "%3c", "%3e", "%3f", "%20", "%23", "%3a", "%C3%A4", "%C3%B6", "%C3%BC", "%C3%9C", "%C3%84", "%C3%96", "%C3%9F", "%2d" }));
351
 
#else
352
 
    return replace(str, 
353
 
                   ({ "�", "�", "�", "�", "�", "�", "�", "<", ">", "?", " ", "#", ":" }),
354
 
                   ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc", "%df", 
355
 
                          "%3c", "%3e", "%3f", "%20", "%23", "%3a" }));
 
390
  string result = "";
 
391
  for ( int i = 0; i < sizeof(str); i++ ) {
 
392
      switch(str[i]) {
 
393
      case '"':
 
394
          result += "%2d";
 
395
          break;
 
396
      case '<':
 
397
          result += "%3c";
 
398
          break;
 
399
      case '>':
 
400
          result += "%3e";
 
401
          break;
 
402
      case '?':
 
403
          result += "%3f";
 
404
          break;
 
405
      case ' ':
 
406
          result += "%20";
 
407
          break;
 
408
      case '#':
 
409
          result += "%23";
 
410
          break;
 
411
      case '&':
 
412
          result += "%26";
 
413
          break;
 
414
      case '+':
 
415
          result += "%2b";
 
416
          break;
 
417
      case ',':
 
418
          result += "%2c";
 
419
          break;
 
420
      case ';':
 
421
          result += "%3b";
 
422
          break;
 
423
      case '@':
 
424
          result += "%40";
 
425
          break;
 
426
      case '$':
 
427
          result += "%24";
 
428
          break;
 
429
      case '%':
 
430
          result += "%25";
 
431
          break;
 
432
      case '{':
 
433
          result += "%7b";
 
434
          break;
 
435
      case '}':
 
436
          result += "%7d";
 
437
          break;
 
438
      case '|':
 
439
          result += "%7c";
 
440
          break;
 
441
      case '\\':
 
442
          result += "%5c";
 
443
          break;
 
444
      case '[':
 
445
          result += "%5b";
 
446
          break;
 
447
      case ']':
 
448
          result += "%5d";
 
449
          break;
 
450
      case '`':
 
451
          result += "%60";
 
452
          break;
 
453
      default:
 
454
          if ( str[i] >= 128 ) {
 
455
              result += sprintf("%%%x", str[i]);
 
456
          }
 
457
          else
 
458
              result += str[i..i];
 
459
      }
 
460
  }
 
461
  return result;
 
462
 
 
463
#if 0
 
464
  return replace(result,
 
465
                 ({ "�", "�", "�", "�", "�", "�", "�", "<", ">", "?", " ", "#", ":", "\303\244", "\303\266", "\303\274", "\303\234", "\303\204", "\303\226", "\303\237", "\"" }),
 
466
                 ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc", "%df",
 
467
                          "%3c", "%3e", "%3f", "%20", "%23", "%3a", "%C3%A4", "%C3%B6", "%C3%BC", "%C3%9C", "%C3%84", "%C3%96", "%C3%9F", "%2d" }));  
356
468
#endif
357
469
}
358
470
 
412
524
{
413
525
  string encoding = "iso-8859-1";
414
526
  
 
527
  if ( !stringp(text) )
 
528
    return encoding;
 
529
  
415
530
  int n = search(text, "charset=");
416
531
  if ( n == -1 ) 
417
532
    n = search(text, "CHARSET=");
440
555
    return replace(text, ({ "\n", "  " }), ({ "<br/>", "&nbsp;" }));
441
556
}
442
557
 
443
 
 
444
558
/**
445
559
 * Return a result page in html with the message in the content
446
560
 * area.
454
568
string 
455
569
result_page(string message, string next, string|void title, string|void head)
456
570
{
457
 
    object xsl;
458
 
    string xml;
459
 
    
460
 
    if ( !stringp(title) ) title = "Result";
461
 
    if ( !stringp(head) ) head = "";
462
 
 
463
 
    xsl = find_object("/stylesheets/result.xsl");
464
 
    xml = "<?xml version='1.0' encoding='utf-8'?>"+
465
 
        "<result>"+
466
 
        "<head><![CDATA["+head+"]]></head>"+
467
 
        "<title>"+title+"</title><actions/>\n<message><![CDATA["+message+
468
 
        "]]></message></result>";
469
 
    return get_module("libxslt")->run(xml, xsl, (["back": next,]) );
 
571
    string html = "";
 
572
    // need a webinterface version check here
 
573
    object web = _Server->get_module("package:web");
 
574
    string wi_version = "0.0.0";
 
575
    if (objectp(web)) wi_version = web->get_version();
 
576
    array wiv = wi_version / ".";
 
577
    // if webinterface version >= 2.1, then use new method respecting user 
 
578
    // stylesheet setting (and may use internationalization in the future..)
 
579
    if ( (sizeof(wiv) > 0 && (int)wiv[0]>2) || (sizeof(wiv) > 1 && (int)wiv[0]>=2 && (int)wiv[1]>=1) ) {
 
580
      object xsl;
 
581
      string xml;
 
582
      
 
583
      if ( !stringp(title) ) title = "Result";
 
584
      if ( !stringp(head) ) head = "";
 
585
  
 
586
      object user = geteuid();
 
587
      if (!objectp(user)) user = this_user();
 
588
      object css_style;
 
589
      if (objectp(user)) css_style = user->query_attribute("USER_CSS_STYLE");
 
590
      xsl = find_object("/stylesheets/result.xsl");
 
591
      xml = "<?xml version='1.0' encoding='utf-8'?>"+
 
592
            "<Object>";
 
593
      if (objectp(css_style)) {
 
594
        xml += "<properties><css_style><Object><path>"+
 
595
               _Server->get_module("filepath:tree")->object_to_filename(css_style)+
 
596
               "</path></Object></css_style></properties>";
 
597
      }
 
598
      xml += "<head><![CDATA["+head+"]]></head>"+
 
599
             "<title>"+title+"</title><actions/>\n<message><![CDATA["+message+
 
600
             "]]></message></Object>";
 
601
      mapping myvars = ([]);
 
602
      myvars["back"] = next;
 
603
      myvars["title"] = title;
 
604
      myvars["head"] = head;
 
605
      myvars["message"] = message;    
 
606
      /*
 
607
      // doesnt work yet, need help and more testing
 
608
      // produces strange error "Start Tag expected..." but xsl.xml file looks fine ?!
 
609
          xml = _Server->get_module("Converter:XML")->get_xml(this(), xsl, myvars, 0);
 
610
      werror("xml="+xml+"\r\n");
 
611
      */
 
612
      html = _Server->get_module("libxslt")->run(xml, xsl, myvars );
 
613
    }
 
614
    else {  // do it the old-fashioned way...
 
615
      object xsl;
 
616
      string xml;
 
617
      
 
618
      if ( !stringp(title) ) title = "Result";
 
619
      if ( !stringp(head) ) head = "";
 
620
  
 
621
      xsl = find_object("/stylesheets/result.xsl");
 
622
      xml = "<?xml version='1.0' encoding='utf-8'?>"+
 
623
          "<result>"+
 
624
          "<head><![CDATA["+head+"]]></head>"+
 
625
          "<title>"+title+"</title><actions/>\n<message><![CDATA["+message+
 
626
          "]]></message></result>";
 
627
      html = _Server->get_module("libxslt")->run(xml, xsl, (["back": next,]) );
 
628
    }
 
629
  return html;
470
630
}
471
631
 
 
632
 
472
633
/**
473
634
 * Return a mapping in appropriate format for Protocols.HTTP to handle.
474
635
 *  
475
636
 * @param int code - the http return code.
476
637
 * @return mapping suitable for Protocols.HTTP
477
638
 */
478
 
mapping low_answer(int code, string response)
 
639
mapping low_answer(int code, string response, void|mapping extra)
479
640
{
480
 
    return ([ "error": code, "data": response, ]);
 
641
 mapping answer = ([ "error": code, "rettext": response, ]);
 
642
 if ( mappingp(extra) ) 
 
643
     answer->extra_heads = extra;
 
644
 return answer;
481
645
}
482
646
 
483
647
/**
494
658
    identify_browser(
495
659
                     vars["__internal"]["client"],
496
660
                     vars["__internal"]["request_headers"]);
497
 
  string l = this_user()->query_attribute(USER_LANGUAGE);
 
661
  object user = this_user();
 
662
  if ( !objectp(user) )
 
663
      return client_map;
 
664
  string l = user->query_attribute(USER_LANGUAGE);
498
665
  if ( stringp(l) )
499
 
        client_map->language = l;
500
 
    return client_map;
 
666
      client_map->language = l;
 
667
  return client_map;
501
668
}
502
669
 
503
670
 
506
673
{
507
674
  mapping c =
508
675
    set_cookie("steam_auth", MIME.encode_base64(user+":"+pass));
509
 
  werror("setting auth cookie: %O\n", c);
510
676
  c["Cache-Control"] = "no-cache=\"set-cookie\"";
511
677
  return c;
512
678
}
557
723
        xml = obj;
558
724
    }
559
725
    else {
560
 
        float t = gauge {
561
 
            xml = get_module("Converter:XML")->get_xml(
562
 
                obj, xsl, vars, (int)vars["active"]);
563
 
        };
 
726
        xml = _Server->get_module("Converter:XML")->get_xml(obj, xsl, 
 
727
                                                 vars, (int)vars["active"]);
564
728
    }
565
729
    if ( vars->source == "true" )
566
730
        return xml;
567
731
    
568
 
    html = get_module("libxslt")->run(xml, xsl, vars);
569
 
        
 
732
    html = _Server->get_module("libxslt")->run(xml, xsl, vars); 
570
733
 
571
734
    return html;
572
735
}
645
808
static object find_user_stylesheet(object user, object obj, string type)
646
809
{
647
810
  object xsl;
648
 
  mapping xslMap = obj->query_attribute("xsl:"+type);
 
811
  mixed xslMap = obj->query_attribute("xsl:"+type);
 
812
 
 
813
  if ( objectp(xslMap) )
 
814
      return xslMap;
649
815
 
650
816
  if ( !mappingp(xslMap) ) 
651
817
    xslMap = ([ ]);
694
860
        object xslcont = xsl->get_environment();
695
861
        if ( !objectp(xslcont) )
696
862
          xslcont = find_object("/stylesheets");
697
 
        MESSAGE("XSL Container is: %s\n", xslcont->get_identifier());
698
863
        xsl = xslcont->get_object_byname(vars->type); 
699
864
      }
700
865
    }
701
 
    if ( !objectp(xsl) ) {
 
866
    if ( !objectp(xsl) || !(xsl->get_object_class() & CLASS_DOCXSL) ) {
702
867
      if ( vars->type == "PDA:content" )
703
868
        vars->type = "zaurus";
704
869
      xsl = _FILEPATH->path_to_object("/stylesheets/"+vars->type+".xsl");
708
873
}
709
874
 
710
875
 
 
876
/**
 
877
 * Get the appropriate stylesheet for an object (usually xml document)
 
878
 * checks for xsl:document
 
879
 *  
 
880
 * @param object obj - the object to show
 
881
 * @return the appropriate stylesheet to be used using xsl:document or xsl:public
 
882
 */
 
883
object get_stylesheet(object obj)
 
884
{
 
885
    object xsl;
 
886
    xsl = find_user_stylesheet(this_user(), obj, "document");
 
887
    if ( !objectp(xsl) ) {
 
888
        if ( obj->query_attribute("xsl:use_public") )
 
889
            return obj->query_attribute("xsl:public");
 
890
        // try to parse ...
 
891
        if ( obj->query_attribute(DOC_MIME_TYPE) == "text/xml" ) {
 
892
            string content = obj->get_content();
 
893
            if ( stringp(content) && content != "" ) {
 
894
              catch {
 
895
                object node = xmlDom.parse(content);
 
896
                mapping pi = node->get_pi();
 
897
                if ( pi["xml-stylesheet"] ) {
 
898
                  array stylesheets;
 
899
 
 
900
                  if ( !arrayp(pi["xml-stylesheet"]) )
 
901
                    stylesheets = ({ pi["xml-stylesheet"] });
 
902
                  else
 
903
                    stylesheets = pi["xml-stylesheet"];
 
904
                  foreach(stylesheets, string stylesheet ) {
 
905
                    string styleref;
 
906
                    object fp = _Server->get_module("filepath:tree");
 
907
                    object env = obj->get_environment();
 
908
                    string type = "text/xsl";
 
909
                    if ( search(stylesheet, "type=") >= 0 )
 
910
                      sscanf(stylesheet, "%*shref=\"%s\"%*s", type);
 
911
                    if ( type == "text/xsl" && 
 
912
                         sscanf(stylesheet, "%*shref=\"%s\"%*s", styleref) )
 
913
                    {
 
914
                      if ( sscanf(styleref, "%s.css", styleref) )
 
915
                        continue; // no css
 
916
 
 
917
                      if ( styleref[0] != '/' )
 
918
                        return fp->resolve_path(env,styleref);
 
919
                      else
 
920
                        return fp->path_to_object(styleref);
 
921
                    }
 
922
                  }
 
923
                }
 
924
              };
 
925
            }
 
926
        }           
 
927
    }
 
928
    return xsl;
 
929
}
711
930
 
712
931
class testTag {
713
932
    string execute(mapping vars) {
745
964
    result = rxml(result, ([ ]), (["a": tagTag(), ]));
746
965
    if ( result != "<BODY><?xml version='1.0'?>\n\n<x>test</x></BODY>" )
747
966
        error("Failed test with rxml tag with xml in body.\n");
748
 
 
749
 
}
750
 
    
 
967
}
 
968
 
 
969
string get_tag_name(object tag)
 
970
{
 
971
  string name = tag->get_identifier();
 
972
  sscanf(name, "%s.pike", name);
 
973
  return name;
 
974
}
 
975
 
 
976
function get_tag_function(object tag)
 
977
{
 
978
  object instance;  
 
979
  if ( !objectp(tag) )
 
980
    return 0;
 
981
  instance = tag->get_instance();
 
982
  if ( !objectp(instance) )
 
983
    return 0;
 
984
 
 
985
  return instance->execute;
 
986
}
 
987
 
 
988
function find_tag(string name)
 
989
{
 
990
  object tags = OBJ("/tags");
 
991
  if ( !objectp(tags) ) 
 
992
    return 0;
 
993
  object tag = tags->get_object_byname(name+".pike");
 
994
  if ( !objectp(tag) )
 
995
    return 0;
 
996
  return get_tag_function(tag);
 
997
}
 
998
 
 
999
 
 
1000
mapping find_tags(object obj)
 
1001
{
 
1002
  if ( !objectp(obj) )
 
1003
    return 0;
 
1004
  if ( obj->get_object_class() & CLASS_CONTAINER ) {
 
1005
    mapping result = ([ ]);
 
1006
    foreach(obj->get_inventory_by_class(CLASS_DOCLPC), object tag) {
 
1007
      function f = get_tag_function(tag);
 
1008
      string tagname = get_tag_name(tag);
 
1009
      if ( !functionp(f) )
 
1010
        MESSAGE("Warning - no tag function for tag: %s", tagname);
 
1011
      else
 
1012
        result[tagname] = f;
 
1013
    }
 
1014
    return result;
 
1015
  }
 
1016
  else if ( obj->get_object_class() & CLASS_DOCXSL) {
 
1017
    object env = obj->get_environment();
 
1018
    if ( objectp(env) )
 
1019
      return find_tags(env->get_object_byname("tags"));
 
1020
  }
 
1021
  return 0;
 
1022
}