Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447 00448 00449 00450 00451 00452 00453 00454 00455 00456 00457 00458 00459 00460 00461 00462 00463 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 00477 00478 00479 00480 00481 00482 00483 00484 00485 00486 00487 00488 00489 00490 00491 00492 00493 00494 00495 00496 00497 00498 00499 00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513 00514 00515 00516 00517 00518 00519 00520 00521 00522 00523 00524 00525 00526 00527 00528 00529 00530 00531 00532 00533 00534 00535 00536 00537 00538 00539 00540 00541 00542 00543 00544 00545 00546 00547 00548 00549 00550 00551 00552 00553 00554 00555 00556 00557 00558 00559 00560 00561 00562 00563 00564 00565 00566 00567 00568 00569 00570 00571 00572 00573 00574 00575 00576 00577 00578 00579 00580 00581 00582 00583 00584 00585 00586 00587 00588 00589 00590 00591 00592 00593 00594 00595 00596 00597 00598 00599 00600 00601 00602 00603 00604 00605 00606 00607 00608 00609 00610 00611 00612 00613 00614 00615 00616 00617 00618 00619 00620 00621 00622 00623 00624 00625 00626 00627 00628 00629 00630 00631 00632 00633 00634 00635 00636 00637 00638 00639 00640 00641 00642 00643 00644 00645 00646 00647 00648 00649 00650 00651 00652 00653 00654 00655 00656 00657 00658 00659 00660 00661 00662 00663 00664 00665 00666 00667 00668 00669 00670 00671 00672 00673 00674 00675 00676 00677 00678 00679 00680 00681 00682 00683 00684 00685 00686 00687 00688 00689 00690 00691 00692 00693 00694 00695 00696 00697 00698 00699 00700 00701 00702 00703 00704 00705 00706 00707 00708 00709 00710 00711 00712 00713 00714 00715 00716 00717 00718 00719 00720 00721 00722 00723 00724 00725 00726 00727 00728 00729 00730 00731 00732 00733 00734 00735 00736 00737 00738 00739 00740 00741 00742 00743 00744 00745 00746 00747 00748 00749 00750 00751 00752 00753 00754 00755 00756 00757 00758 00759 00760 00761 00762 00763 00764 00765 00766 00767 00768 00769 00770 00771 00772 00773 00774 00775 00776 00777 00778 00779 00780 00781 00782 00783 00784 00785 00786 00787 00788 00789 00790 00791 00792 00793 00794 00795 00796 00797 00798 00799 00800 00801 00802 00803 00804 00805 00806 00807 00808 00809 00810 00811 00812 00813 00814 00815 00816 00817 00818 00819 00820 00821 00822 00823 00824 00825 00826 00827 00828 00829 00830 00831 00832 00833 00834 00835 00836 00837 00838 00839 00840 00841 00842 00843 00844 00845 00846 00847 00848 00849 00850 00851 00852 00853 00854 00855 00856 00857 00858 00859 00860 00861 00862 00863 00864 00865 00866 00867 00868 00869 00870 00871 00872 00873 00874 00875 00876 00877 00878 00879 00880 00881 00882 00883 00884 00885 00886 00887 00888 00889 00890 00891 00892 00893 00894 00895 00896 00897 00898 00899 00900 00901 00902 00903 00904 00905 00906 00907 00908 00909 00910 00911 00912 00913 00914 00915 00916 00917 00918 00919 00920 00921 00922 00923 00924 00925 00926 00927 00928 00929 00930 00931 00932 00933 00934 00935 00936 00937 00938 00939 00940 00941 00942 00943 00944 00945 00946 00947 00948 00949 00950 00951 00952 00953 00954 00955 00956 00957 00958 00959 00960 00961 00962 00963 00964 00965 00966 00967 00968 00969 00970 00971 00972 00973 00974 00975 00976 00977 00978 00979 00980 00981 00982 00983 00984 00985 00986 00987 00988 00989 00990 00991 00992 00993 00994 00995 00996 00997 00998 00999 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 01012 01013 01014 01015 01016 01017 01018 01019 01020 01021 01022 01023 01024 01025 01026 01027 01028 01029 01030 01031 01032 01033 01034 01035 01036 01037 01038 01039 01040 01041 01042 01043 01044 01045 01046 01047 01048 01049 01050 01051 01052 01053 01054 01055 01056 01057 01058 01059 01060 01061 01062 01063 01064 01065 01066 01067 01068 01069 01070 01071 01072 01073 01074 01075 01076 01077 01078 01079 01080 01081 01082 01083 01084 01085 01086 01087 01088 01089 01090 01091 01092 01093 01094 01095 01096 01097 01098 01099 01100 01101 01102 01103 01104 01105 01106 01107 01108 01109 01110 01111 01112 01113 01114 01115 01116 01117 01118 01119 01120 01121 01122 01123 01124 01125 01126 01127 01128 01129 01130 01131 01132 01133 01134 01135 01136 01137 01138 01139 01140 01141 01142 01143 01144 01145 01146 01147 01148 01149 01150 01151 01152 01153 01154 01155 01156 01157 01158 01159 01160 01161 01162 01163 01164 01165 01166 01167 01168 01169 01170 01171 01172 01173 01174 01175 01176 01177 01178 01179 01180 01181 01182 01183 01184 01185 01186 01187 01188 01189 01190 01191 01192 01193 01194 01195 01196 01197 01198 01199 01200 01201 01202 01203 01204 01205 01206 01207 01208 01209 01210 01211 01212 01213 01214 01215 01216 01217 01218 01219 01220 01221 01222 01223 01224 01225 01226 01227 01228 01229 01230 01231 01232 01233 01234 01235 01236 01237 01238 01239 01240 01241 01242 01243 01244 01245 01246 01247 01248 01249 01250 01251 01252 01253 01254 01255 01256 01257 01258 01259 01260 01261 01262 01263 01264 |
/******************************************************************************* HttpUtil <br /> Miscelaneous static functions. Part of [[LibHTTP]]. <br /> Contains various algorithms, under which [[Base64]] encoding and [[MD5]] hash generation. <br /> [[MD5]] code by Petr Jelinek ( http://wiki.beyondunreal.com/wiki/MD5 ) <br /> <br /> Dcoumentation and Information: http://wiki.beyondunreal.com/wiki/LibHTTP <br /> <br /> Authors: Michiel 'El Muerte' Hendriks <elmuerte@drunksnipers.com> <br /> <br /> Copyright 2003, 2004 Michiel "El Muerte" Hendriks <br /> Released under the Lesser Open Unreal Mod License <br /> http://wiki.beyondunreal.com/wiki/LesserOpenUnrealModLicense <!-- $Id: HttpUtil.uc,v 1.21 2005/12/13 11:36:08 elmuerte Exp $ --> *******************************************************************************/ class HttpUtil extends Core.Object; /* log levels */ var const int LOGERR; var const int LOGWARN; var const int LOGINFO; var const int LOGDATA; /** month names to use for date string generation */ var const string MonthNames[13]; /** names of the days, 0 = sunday */ var const string DayNamesLong[7], DayNamesShort[7]; /** days offsets for each month*/ var const int MonthOffset[13], MonthOffsetLeap[13]; struct DateTime { var int year; var int month; var int day; var int weekday; var int hour; var int minute; var int second; }; /** MD5 context */ struct MD5_CTX { /** state (ABCD) */ var array<int> state; /** number of bits, modulo 2^64 (lsb first) */ var array<int> count; /** input buffer */ var array<byte> buffer; }; /** a better URL structure that contains all elements */ struct xURL { /** protocol used, like http:/ /, https:/ /, ftp:/ /. But without the ':/ /' part. (ignore the space between the two slashes, it's required because of a bug in UE2) */ var string protocol; /** username that was provided in the URL */ var string username; /** possible password that was in the url */ var string password; /** the hostname */ var string hostname; /** the port number specified in the URL */ var int port; /** the location field, all from the hostname up to the query or hash string, includes leading / */ var string location; /** the part after the ?, without the leading ? */ var string query; /** the part after the #, without the leading # */ var string hash; }; /** URL delimiter */ const TOKEN_PATH = "/"; /** URL delimiter */ const TOKEN_HASH = "#"; /** URL delimiter */ const TOKEN_QUERY = "?"; /** URL delimiter; to seperate protocol from the rest */ const TOKEN_PROTOCOL = "://"; /** URL delimiter */ const TOKEN_USER = "@"; /** URL delimiter; to seperate the user and pass from the url */ const TOKEN_USERPASS = ":"; /** URL delimiter */ const TOKEN_PORT = ":"; /** URL escape token */ const URL_ESCAPE = "%"; /** Encode special characters, you should not use this function, it's slow and not secure, so try to avoid it. ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" and " " */ static final function string RawUrlEncode(string instring) { ReplaceChar(instring, ";", "%3B"); ReplaceChar(instring, "/", "%2F"); ReplaceChar(instring, "?", "%3F"); ReplaceChar(instring, ":", "%3A"); ReplaceChar(instring, "@", "%40"); ReplaceChar(instring, "&", "%26"); ReplaceChar(instring, "=", "%3D"); ReplaceChar(instring, "+", "%2B"); ReplaceChar(instring, ",", "%2C"); ReplaceChar(instring, "$", "%24"); ReplaceChar(instring, " ", "%20"); return instring; } /** This will decode URL encoded elements. If bIgnorePlus is set to true '+' will not be changed to a space */ static final function string RawUrlDecode(string instring, optional bool bIgnorePlus) { local int i; local string char; if (!bIgnorePlus) ReplaceChar(instring, "+", " "); i = InStr(instring, URL_ESCAPE); while (i > -1) { char = mid(instring, i+1, 2); char = chr(HexToDec(char)); if (char == "%") char = chr(1); instring = Left(instring, i)$char$Mid(instring, i+3); i = InStr(instring, URL_ESCAPE); } ReplaceChar(instring, chr(1), URL_ESCAPE); // % was replace with \1 return instring; } /** parses the inURL to an xURL structure, return true when succesful */ static final function bool parseUrl(string inURL, out xURL outURL) { local int i, j; i = InStr(inURL, TOKEN_PROTOCOL); if (i == -1) return false; if (i == 0) return false; // empty protocol outURL.protocol = Left(inURL, i); inURL = mid(inURL, i+len(TOKEN_PROTOCOL)); i = InStr(inURL, TOKEN_USER); if (i == -1) { outURL.username = ""; outURL.password = ""; } else { outURL.username = Left(inURL, i); inURL = mid(inURL, i+len(TOKEN_USER)); i = InStr(outURL.username, TOKEN_USERPASS); if (i == -1) { outURL.password = ""; } else { outURL.password = Mid(outURL.username, i+len(TOKEN_USERPASS)); outURL.username = Left(outURL.username, i); } outURL.username = RawUrlDecode(outURL.username); outURL.password = RawUrlDecode(outURL.password); } if (inURL == "") return false; i = InStr(inURL, TOKEN_PATH); if (i == -1) // just protocol://hostname { outURL.hostname = inURL; outURL.hash = ""; outURL.query = ""; outURL.location = TOKEN_PATH; return true; } if (i == 0) return false; // e.g. http:///, also traps file:/// but we don't give a crap about that outURL.hostname = Left(inURL, i); inURL = mid(inURL, i); // now it contains location(\?query)?(#hash)? i = InStr(outURL.hostname, TOKEN_PORT); if (i == 0) return false; // http://:80/ !? outURL.port = -1; if (i > 0) { j = int(mid(outURL.hostname, i+1)); if (outURL.port == 0) // invalid port no { return false; } else if (j != getPortByProtocol(outURL.protocol)) { outURL.port = j; } outURL.hostname = Left(outURL.hostname, i); } i = InStr(inURL, TOKEN_HASH); if (i != -1) { outURL.hash = Mid(inURL, i+len(TOKEN_HASH)); inURL = left(inURL, i); } else outURL.hash = ""; i = InStr(inURL, TOKEN_QUERY); if (i != -1) { outURL.query = Mid(inURL, i+len(TOKEN_QUERY)); inURL = left(inURL, i); } else outURL.query = ""; outURL.location = inURL; return true; } /** converts a xURL to a string. bIncludePassword defaults to false */ static final function string xURLtoString(xURL inURL, optional bool bIncludePassword) { local string r; r = inURL.protocol$TOKEN_PROTOCOL; if (inURL.username != "") { r $= RawUrlEncode(inURL.username); if (inURL.password != "" && bIncludePassword) r $= TOKEN_USERPASS$RawUrlEncode(inURL.password); r $= TOKEN_USER; } r $= inURL.hostname; if (inURL.port != -1) r $= TOKEN_PORT$string(inURL.port); if (inURL.location == "") inURL.location = TOKEN_PATH; r $= inURL.location; if (inURL.query != "") r $= TOKEN_QUERY$inURL.query; if (inURL.hash != "") r $= TOKEN_HASH$inURL.hash; return r; } /** convert a xURL to a location string, just the location+query+hash */ static final function string xURLtoLocation(xURL inURL, optional bool bIncludePassword) { local string r; r = inURL.location; if (inURL.query != "") r $= TOKEN_QUERY$inURL.query; if (inURL.hash != "") r $= TOKEN_HASH$inURL.hash; return r; } /** return the default port based on the protocol */ static final function int getPortByProtocol(string protocol) { switch (protocol) { /* case "ftp": return 21; case "ssh": return 22; case "telnet": return 23; case "gopher": return 70; */ case "http": return 80; case "https": return 443; } return 0; } /** replace part of a string */ static final function ReplaceChar(out string instring, string from, string to) { //#ifdef UE2 local int i; local string src; src = instring; instring = ""; i = InStr(src, from); while (i > -1) { instring = instring$Left(src, i)$to; src = Mid(src, i+Len(from)); i = InStr(src, from); } instring = instring$src; //#endif //#ifdef UE5 //instring = repl(instring, from, to); //#endif } /** base64 encode an input array */ static final function array<string> Base64Encode(array<string> indata, out array<string> B64Lookup) { local array<string> result; local int i, dl, n; local string res; local array<byte> inp; local array<string> outp; if (B64Lookup.length != 64) Base64EncodeLookupTable(B64Lookup); // convert string to byte array for (n = 0; n < indata.length; n++) { res = indata[n]; outp.length = 0; inp.length = 0; for (i = 0; i < len(res); i++) { inp[inp.length] = Asc(Mid(res, i, 1)); } dl = inp.length; // fix byte array if ((dl%3) == 1) { inp[inp.length] = 0; inp[inp.length] = 0; } if ((dl%3) == 2) { inp[inp.length] = 0; } i = 0; while (i < dl) { outp[outp.length] = B64Lookup[(inp[i] >> 2)]; outp[outp.length] = B64Lookup[((inp[i]&3)<<4) | (inp[i+1]>>4)]; outp[outp.length] = B64Lookup[((inp[i+1]&15)<<2) | (inp[i+2]>>6)]; outp[outp.length] = B64Lookup[(inp[i+2]&63)]; i += 3; } // pad result if ((dl%3) == 1) { outp[outp.length-1] = "="; outp[outp.length-2] = "="; } if ((dl%3) == 2) { outp[outp.length-1] = "="; } res = ""; for (i = 0; i < outp.length; i++) { res = res$outp[i]; } result[result.length] = res; } return result; } /** Decode a base64 encoded string */ static final function array<string> Base64Decode(array<string> indata) { local array<string> result; local int i, dl, n, padded; local string res; local array<byte> inp; local array<string> outp; // convert string to byte array for (n = 0; n < indata.length; n++) { res = indata[n]; outp.length = 0; inp.length = 0; padded = 0; for (i = 0; i < len(res); i++) { dl = Asc(Mid(res, i, 1)); // convert base64 ascii to base64 index if ((dl >= 65) && (dl <= 90)) dl -= 65; // cap alpha else if ((dl >= 97) && (dl <= 122)) dl -= 71; // low alpha else if ((dl >= 48) && (dl <= 57)) dl += 4; // digits else if (dl == 43) dl = 62; else if (dl == 47) dl = 63; else if (dl == 61) padded++; inp[inp.length] = dl; } dl = inp.length; i = 0; while (i < dl) { outp[outp.length] = Chr((inp[i] << 2) | (inp[i+1] >> 4)); outp[outp.length] = Chr(((inp[i+1]&15)<<4) | (inp[i+2]>>2)); outp[outp.length] = Chr(((inp[i+2]&3)<<6) | (inp[i+3])); i += 4; } outp.length = outp.length-padded; res = ""; for (i = 0; i < outp.length; i++) { res = res$outp[i]; } result[result.length] = res; } return result; } /** Generate the base 64 encode lookup table */ static final function Base64EncodeLookupTable(out array<string> LookupTable) { local int i; for (i = 0; i < 26; i++) { LookupTable[i] = Chr(i+65); } for (i = 0; i < 26; i++) { LookupTable[i+26] = Chr(i+97); } for (i = 0; i < 10; i++) { LookupTable[i+52] = Chr(i+48); } LookupTable[62] = "+"; LookupTable[63] = "/"; } /** Create a UNIX timestamp. <br /> Warning: Assumes info is passed in GMT. So make sure you correct the timezone if you are going to send a timestamp generated with this function to an other server. */ static final function int timestamp(int year, int mon, int day, int hour, int min, int sec) { /* Origin of the algorithm below: Linux Kernel <time.h> */ mon -= 2; if (mon <= 0) { /* 1..12 -> 11,12,1..10 */ mon += 12; /* Puts Feb last since it has leap day */ year -= 1; } return ((( (year/4 - year/100 + year/400 + 367*mon/12 + day) + year*365 - 719499 )*24 + hour /* now have hours */ )*60 + min /* now have minutes */ )*60 + sec; /* finally seconds */ } /** Parse a string to a timestamp The date string is formatted as: Wdy, DD-Mon-YYYY HH:MM:SS GMT TZoffset is the local offset to GMT */ static final function int stringToTimestamp(string datestring, optional int TZoffset) { local array<string> data, datePart, timePart; local int i; local float tzoff; datestring = trim(datestring); split(datestring, " ", data); if (data.length == 6) // date is in spaced format { data[1] = data[1]$"-"$data[2]$"-"$data[3]; data[2] = data[4]; data[3] = data[5]; data.length = 4; } if (data.length == 4) { if (split(data[1], "-", datePart) != 3) return 0; if (split(data[2], ":", timePart) != 3) return 0; // find month offset for (i = 1; i < ArrayCount(default.MonthNames); i++) { if (default.MonthNames[i] ~= datePart[1]) { datePart[1] = string(i); break; } } if (Len(datePart[2]) == 2) datePart[2] = "20"$datePart[2]; tzoff = TZtoOffset(data[3]); return timestamp(int(datePart[2]), int(datePart[1]), int(datePart[0]), int(timePart[0])+TZoffset+int(tzoff), int(timePart[1])+(tzoff%1*60), int(timePart[2])); } return 0; } /** returns if year is a leap year */ static final function bool isLeapYear(int year) { return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0); } /** returns the number of days in a year */ static final function int daysInYear(int year) { const DAYS_PER_YEAR = 365; const DAYS_PER_LEAP_YEAR = 366; if (isLeapYear(year)) return DAYS_PER_LEAP_YEAR; else return DAYS_PER_YEAR; } static final function int leapsThruEndOf(int y) { return ((y / 4) - (y / 100) + (y / 400)); } /** the float % operator is broken for our needs (numbers >2^24) */ static final operator(18) int % ( int x, int y ) { return x-(x/y*y); } /** converts a timestamp to a DateTime record */ static final function DateTime timestampToDatetime(int timestamp) { /* Origin of the algorithm below: GNU C Library <offtime.c> */ const SECS_PER_DAY = 86400; const SECS_PER_HOUR = 3600; local int days, rem, yg; local DateTime dt; days = timestamp / SECS_PER_DAY; rem = timestamp % SECS_PER_DAY; while (rem < 0) { rem += SECS_PER_DAY; --days; } while (rem >= SECS_PER_DAY) { rem -= SECS_PER_DAY; ++days; } dt.hour = rem / SECS_PER_HOUR; rem = rem % SECS_PER_HOUR; dt.minute = rem / 60; dt.second = rem % 60; /* January 1, 1970 was a Thursday. */ dt.weekday = (4 + days) % 7; if (dt.weekday < 0) dt.weekday += 7; dt.year = 1970; while (days < 0 || days >= daysInYear(dt.year)) { /* Guess a corrected year, assuming 365 days per year. */ yg = dt.year + days / DAYS_PER_YEAR - int(days % DAYS_PER_YEAR < 0); /* Adjust DAYS and Y to match the guessed year. */ days -= ((yg - dt.year) * DAYS_PER_YEAR + leapsThruEndOf(yg - 1) - leapsThruEndOf(dt.year - 1)); dt.year = yg; } if (isLeapYear(dt.year)) { for (yg = 11; days < default.MonthOffsetLeap[yg]; --yg) continue; dt.month = yg; days -= default.MonthOffsetLeap[yg]; } else { for (yg = 11; days < default.MonthOffset[yg]; --yg) continue; dt.month = yg; days -= default.MonthOffset[yg]; } dt.day = days + 1; return dt; } /** convert a timestamp into a string. <br /> Format can be one of the following strings: <br /> "822", "1123" : RFC 822, updated by RFC 1123 (default), timezone is the TZ CODE <br /> "850", "1036" : RFC 850, obsoleted by RFC 1036, timezone is the TZ CODE <br /> "2822" : RFC 2822, timezone is a +0000 like string <br /> "asctime": ANSI C's asctime() format, timezone is an integer that will increment the hour */ static final function string timestampToString(int timestamp, optional string Timezone, optional string format) { local DateTime dt; dt = timestampToDatetime(timestamp); switch (format) { case "850": case "1036": if (Timezone == "") Timezone = "GMT"; format = default.DayNamesLong[dt.weekday]$", "$Right("0"$dt.day, 2)$"-"$default.MonthNames[dt.month+1]$"-"$dt.Year@Right("0"$dt.hour, 2)$":"$Right("0"$dt.minute, 2)$":"$Right("0"$dt.second, 2)@Timezone; return format; case "asctime": dt.hour += int(Timezone); dt.minute += int(float(Timezone) % 1 * 60); format = default.DayNamesShort[dt.weekday]@default.MonthNames[dt.month+1]@Right(" "$dt.day, 2)@Right("0"$dt.hour, 2)$":"$Right("0"$dt.minute, 2)$":"$Right("0"$dt.second, 2)@dt.year; return format; case "2822": if (Timezone == "") Timezone = "+0000"; format = default.DayNamesShort[dt.weekday]$", "$Right("0"$dt.day, 2)@default.MonthNames[dt.month+1]@dt.Year@Right("0"$dt.hour, 2)$":"$Right("0"$dt.minute, 2)$":"$Right("0"$dt.second, 2)@Timezone; return format; // case: 822 // case: 1123 default: if (Timezone == "") Timezone = "GMT"; format = default.DayNamesShort[dt.weekday]$", "$Right("0"$dt.day, 2)@default.MonthNames[dt.month+1]@dt.Year@Right("0"$dt.hour, 2)$":"$Right("0"$dt.minute, 2)$":"$Right("0"$dt.second, 2)@Timezone; return format; } } /** Converts a timezone code to an offset. */ static final function float TZtoOffset(string TZ) { TZ = Caps(TZ); switch (TZ) { case "GMT": // Greenwich Mean case "UT": // Universal (Coordinated) case "UTC": case "WET": // Western European return 0; case "WAT": // West Africa case "AT": // Azores return -1; //case "BST": // Brazil Standard //case "GST": // Greenland Standard //case "NFT": // Newfoundland //case "NST": // ewfoundland Standard // return -3; case "AST": // Atlantic Standard return -4; case "EST": // Eastern Standard return -5; case "CST": // Central Standard return -6; case "MST": // Mountain Standard return -7; case "PST": // acific Standard return -8; case "YST": // Yukon Standard return -9; case "HST": // Hawaii Standard case "CAT": // Central Alaska case "AHST": // Alaska-Hawaii Standard return -10; case "NT": // Nome return -11; case "IDLW": // International Date Line West return -12; case "CET": // Central European case "MET": // Middle European case "MEWT": // Middle European Winter case "SWT": // Swedish Winter case "FWT": // French Winter return 1; case "CEST": // Central European Summer case "EET": // Eastern Europe, USSR Zone 1 return 2; case "BT": // Baghdad, USSR Zone 2 return 3; //case "IT": // Iran // return 3.5; case "ZP4": // USSR Zone 3 return 4; case "ZP5": // USSR Zone 4 return 5; case "IST": // Indian Standard return 5.5; case "ZP6": // USSR Zone 5 return 6; //case "NST": // North Sumatra // return 6.5; //case "SST": // South Sumatra, USSR Zone 6 case "WAST": // West Australian Standard return 7; //case "JT": // ava (3pm in Cronusland!) // return 7.5; case "CCT": // China Coast, USSR Zone 7 return 8; case "JST": // Japan Standard, USSR Zone 8 return 9; //case "CAST": // Central Australian Standard // return 9.5; case "EAST": // Eastern Australian Standard case "GST": // Guam Standard, USSR Zone 9 return 10; case "NZT": // New Zealand case "NZST": // New Zealand Standard case "IDLE": // International Date Line East return 12; } return int(tz); } /** Trim leading and trailing spaces */ static final function string Trim(coerce string S) { while (Left(S, 1) == " ") S = Right(S, Len(S) - 1); while (Right(S, 1) == " ") S = Left(S, Len(S) - 1); return S; } /** Write a log entry */ static final function Logf(name Comp, coerce string message, optional int level, optional coerce string Param1, optional coerce string Param2) { message = message@chr(9)@param1@chr(9)@Param2; if (Len(message) > 512) message = Left(message, 512)@"..."; // trim message (crash protection) Log(Comp$"["$level$"] :"@message, 'LibHTTP'); } /** get the dirname of a filename, with traling slash */ static final function string dirname(string filename) { local array<string> parts; local int i; split(filename, "/", parts); filename = ""; for (i = 0; i < parts.length-1; i++) { filename = filename$parts[i]$"/"; } return filename; } /** get the base filename */ static final function string basename(string filename) { local array<string> parts; if (split(filename, "/", parts) > 0) return parts[parts.length-1]; return filename; } /** convert a hexadecimal number to the integer counterpart */ static final function int HexToDec(string hexcode) { local int res, i, cur; res = 0; hexcode = Caps(hexcode); for (i = 0; i < len(hexcode); i++) { cur = Asc(Mid(hexcode, i, 1)); if (cur == 32) return res; cur -= 48; // 0 = ascii 30 if (cur > 9) cur -= 7; if ((cur > 15) || (cur < 0)) return -1; // not possible res = res << 4; res += cur; } return res; } /** return the description of a HTTP response code*/ static final function string HTTPResponseCode(int code) { switch (code) { case 100: return "continue"; case 101: return "Switching Protocols"; case 200: return "OK"; case 201: return "Created"; case 202: return "Accepted"; case 203: return "Non-Authoritative Information"; case 204: return "No Content"; case 205: return "Reset Content"; case 206: return "Partial Content"; case 300: return "Multiple Choices"; case 301: return "Moved Permanently"; case 302: return "Found"; case 303: return "See Other"; case 304: return "Not Modified"; case 305: return "Use Proxy"; case 307: return "Temporary Redirect"; case 400: return "Bad Request"; case 401: return "Unauthorized"; case 402: return "Payment Required"; case 403: return "Forbidden"; case 404: return "Not Found"; case 405: return "Method Not Allowed"; case 406: return "Not Acceptable"; case 407: return "Proxy Authentication Required"; case 408: return "Request Time-out"; case 409: return "Conflict"; case 410: return "Gone"; case 411: return "Length Required"; case 412: return "Precondition Failed"; case 413: return "Request Entity Too Large"; case 414: return "Request-URI Too Large"; case 415: return "Unsupported Media Type"; case 416: return "Requested range not satisfiable"; case 417: return "Expectation Failed"; case 500: return "Internal Server Error"; case 501: return "Not Implemented"; case 502: return "Bad Gateway"; case 503: return "Service Unavailable"; case 504: return "Gateway Time-out"; } return ""; } /** Split a string with quotes, quotes may appear anywhere in the string, escape the quote char with a \ to use a literal. <br /> Qoutes are removed from the result, and escaped quotes are used as normal quotes. */ static function int AdvSplit(string input, string delim, out array<string> elm, optional string quoteChar) { local int di, qi; local int delimlen, quotelen; local string tmp; // if quotechar is empty use the faster split method if (quoteChar == "") return Split(input, delim, elm); delimlen = Len(delim); quotelen = Len(quoteChar); ReplaceChar(input, "\\"$quoteChar, chr(1)); // replace escaped quotes while (Len(input) > 0) { di = InStr(input, delim); qi = InStr(input, quoteChar); if (di == -1 && qi == -1) // neither found { ReplaceChar(input, chr(1), quoteChar); elm[elm.length] = input; input = ""; } else if ((di < qi) && (di != -1) || (qi == -1)) // delim before a quotechar { tmp = Left(input, di); ReplaceChar(tmp, chr(1), quoteChar); elm[elm.length] = tmp; input = Mid(input, di+delimlen); } else { tmp = ""; // everything before the quote if (qi > 0) tmp = Left(input, qi); input = mid(input, qi+quotelen); // up to the next quote qi = InStr(input, quoteChar); if (qi == -1) qi = Len(input); tmp = tmp$Left(input, qi); input = mid(input, qi+quotelen); // everything after the quote till delim di = InStr(input, delim); if (di > -1) { tmp = tmp$Left(input, di); input = mid(input, di+delimlen); } ReplaceChar(tmp, chr(1), quoteChar); elm[elm.length] = tmp; } } return elm.length; } /* UnrealScript MD5 routine by Petr Jelinek (PJMODOS) http://wiki.beyondunreal.com/wiki/MD5 Code used for the digest authentication method. One change has been made: the md5 returned is lowercase */ /** return the MD5 of the input string */ static final function string MD5String (string str) { local MD5_CTX context; local array<byte> digest; local string Hex; local int i; MD5Init (context); MD5Update (context, str, Len(str)); digest.Length = 16; MD5Final (digest, context); for (i = 0; i < 16; i++) Hex = Hex $ DecToHex(digest[i], 1); return Hex; } /** Return the MD5 of the input string array. Concat is added after each line. */ static final function string MD5StringArray (array<string> stra, optional string Concat) { local MD5_CTX context; local array<byte> digest; local string Hex, str; local int i; MD5Init (context); for (i = 0; i < stra.length; i++) { str = stra[i]$concat; MD5Update (context, str, Len(str)); } digest.Length = 16; MD5Final (digest, context); for (i = 0; i < 16; i++) Hex = Hex $ DecToHex(digest[i], 1); return Hex; } /** initialize the MD5 context */ static final function MD5Init(out MD5_CTX context) { context.count.Length = 2; context.count[0] = 0; context.count[1] = 0; context.state.Length = 4; context.state[0] = 0x67452301; context.state[1] = 0xefcdab89; context.state[2] = 0x98badcfe; context.state[3] = 0x10325476; context.buffer.Length = 64; } static final function MD5Transform(out array<int> Buf, array<byte> block) { local int A,B,C,D; local array<int> x; A = Buf[0]; B = Buf[1]; C = Buf[2]; D = Buf[3]; x.Length = 16; MD5Decode (x, block, 64); /* Round 1 */ FF (a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */ FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */ FF (c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */ FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */ FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */ FF (d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */ FF (c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */ FF (b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */ FF (a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */ FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */ FF (c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */ FF (b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */ FF (a, b, c, d, x[12], 7, 0x6b901122); /* 13 */ FF (d, a, b, c, x[13], 12, 0xfd987193); /* 14 */ FF (c, d, a, b, x[14], 17, 0xa679438e); /* 15 */ FF (b, c, d, a, x[15], 22, 0x49b40821); /* 16 */ /* Round 2 */ GG (a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */ GG (d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */ GG (c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */ GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */ GG (a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */ GG (d, a, b, c, x[10], 9, 0x2441453); /* 22 */ GG (c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */ GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */ GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */ GG (d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */ GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */ GG (b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */ GG (a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */ GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */ GG (c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */ GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */ /* Round 3 */ HH (a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */ HH (d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */ HH (c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */ HH (b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */ HH (a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */ HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */ HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */ HH (b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */ HH (a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */ HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */ HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */ HH (b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */ HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */ HH (d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */ HH (c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */ HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */ /* Round 4 */ II (a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */ II (d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */ II (c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */ II (b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */ II (a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */ II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */ II (c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */ II (b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */ II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */ II (d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */ II (c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */ II (b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */ II (a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */ II (d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */ II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */ II (b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */ Buf[0] += A; Buf[1] += B; Buf[2] += C; Buf[3] += D; } /** update MD5 context */ static final function MD5Update(out MD5_CTX Context, string Data, int inputLen) { local int i, index, partlen; local array<byte> tmpbuf; tmpbuf.Length = 64; index = ((context.count[0] >>> 3) & 0x3F); if ((context.count[0] += (inputLen << 3)) < (inputLen << 3)) context.count[1]++; context.count[1] += (inputLen >>> 29); partLen = 64 - index; if (inputLen >= partLen) { MD5Move(Data, 0, context.buffer, index, partLen); MD5Transform (context.state, context.buffer); for (i = partLen; i + 63 < inputLen; i += 64) { MD5Move(Data, i, tmpbuf, 0, 64); MD5Transform (context.state, tmpbuf); } index = 0; } else i = 0; MD5Move(Data, i, context.buffer, index, inputLen-i); } /** finalize the MD5 context */ static final function MD5Final (out array<byte> digest, out MD5_CTX context) { local array<byte> bits; local int i, index, padLen; local string strbits; local string PADDING; PADDING = chr(0x80); for (i = 1; i < 64; i++) PADDING = PADDING$chr(0); MD5Encode (bits, context.count, 8); index = ((context.count[0] >>> 3) & 0x3f); if (index < 56) padLen = (56 - index); else padLen = (120 - index); MD5Update (context, PADDING, padLen); strbits = ""; for (i=0;i<8;i++) strbits = strbits$Chr(bits[i]); MD5Update (context, strbits, 8); MD5Encode (digest, context.state, 16); for (i = 0; i < 64; i++) { context.buffer[i] = 0; } } static final function MD5Encode (out array<byte> output, array<int> input, int len) { local int i, j; i = 0; for (j = 0; j < len; j += 4) { output[j] = (input[i] & 0xff); output[j+1] = ((input[i] >> 8) & 0xff); output[j+2] = ((input[i] >> 16) & 0xff); output[j+3] = ((input[i] >> 24) & 0xff); i++; } } static final function MD5Decode(out array<int> output, array<byte> input, int len) { local int i, j; i = 0; for (j = 0; j < len; j += 4) { output[i] = ((input[j]) | (int(input[j+1]) << 8) | (int(input[j+2]) << 16) | (int(input[j+3]) << 24)); i++; } } static final function MD5Move(string src, int srcindex, out array<byte> buffer, int bufindex, int len) { local int i,j; j = bufindex; for (i = srcindex; i < srcindex+len; i++) { buffer[j] = Asc(Mid(src, i, 1)); j++; if (j == 64) break; } } static final function int ROTATE_LEFT (int x, int n) { return (((x) << (n)) | ((x) >>> (32-(n)))); } static final function int F (int x, int y, int z) { return (((x) & (y)) | ((~x) & (z))); } static final function int G (int x, int y, int z) { return ((x & z) | (y & (~z))); } static final function int H (int x, int y, int z) { return (x ^ y ^ z); } static final function int I (int x, int y, int z) { return (y ^ (x | (~z))); } static final function FF(out int a, int b, int c, int d, int x, int s, int ac) { a += F(b, c, d) + x + ac; a = ROTATE_LEFT (a, s); a += b; } static final function GG(out int a, int b, int c, int d, int x, int s, int ac) { a += G(b, c, d) + x + ac; a = rotate_left (a, s) +b; } static final function HH(out int a, int b, int c, int d, int x, int s, int ac) { a += H(b, c, d) + x + ac; a = rotate_left (a, s) +b; } static final function II(out int a, int b, int c, int d, int x, int s, int ac) { a += I(b, c, d) + x + ac; a = rotate_left (a, s) +b; } /** convert a decimal to hexadecimal notation */ static final function string DecToHex(int dec, int size) { const hex = "0123456789abcdef"; local string s; local int i; for (i = 0; i < size*2; i++) { s = mid(hex, dec & 0xf, 1) $ s; dec = dec >>> 4; } return s; } defaultproperties { LOGERR=0 LOGWARN=1 LOGINFO=2 LOGDATA=3 MonthNames[1]="Jan" MonthNames[2]="Feb" MonthNames[3]="Mar" MonthNames[4]="Apr" MonthNames[5]="May" MonthNames[6]="Jun" MonthNames[7]="Jul" MonthNames[8]="Aug" MonthNames[9]="Sep" MonthNames[10]="Oct" MonthNames[11]="Nov" MonthNames[12]="Dec" DayNamesShort[0]="Sun" DayNamesShort[1]="Mon" DayNamesShort[2]="Tue" DayNamesShort[3]="Wed" DayNamesShort[4]="Thu" DayNamesShort[5]="Fri" DayNamesShort[6]="Sat" DayNamesLong[0]="Sunday" DayNamesLong[1]="Monday" DayNamesLong[2]="Tuesday" DayNamesLong[3]="Wednesday" DayNamesLong[4]="Thursday" DayNamesLong[5]="Friday" DayNamesLong[6]="Saturday" MonthOffset[0]=0 MonthOffset[1]=31 MonthOffset[2]=59 MonthOffset[3]=90 MonthOffset[4]=120 MonthOffset[5]=151 MonthOffset[6]=181 MonthOffset[7]=212 MonthOffset[8]=243 MonthOffset[9]=273 MonthOffset[10]=304 MonthOffset[11]=334 MonthOffset[12]=365 MonthOffsetLeap[0]=0 MonthOffsetLeap[1]=31 MonthOffsetLeap[2]=60 MonthOffsetLeap[3]=91 MonthOffsetLeap[4]=121 MonthOffsetLeap[5]=152 MonthOffsetLeap[6]=182 MonthOffsetLeap[7]=213 MonthOffsetLeap[8]=244 MonthOffsetLeap[9]=274 MonthOffsetLeap[10]=305 MonthOffsetLeap[11]=335 MonthOffsetLeap[12]=366 } |
Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |