1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
|
<html>
<head>
<title>AOLserver</title>
</head>
<body>
<a name=top><h1>Connection and Socket Functions</h1></a>
<p>
<small>
$Header: /cvsroot/aolserver/aolserver.com/docs/devel/tcl/api/conn.html,v 1.1 2002/03/07 19:15:35 kriston Exp $
</small>
<p>
<h2><a href=./ name=ns_checkurl>ns_checkurl</a></h2>
Authorize a request using basic HTTP authentication.
<h3>Syntax</h3>
ns_checkurl method url authuser authpasswd ?ipaddr?
<h3>Description</h3>
This function does the same permission check that the AOLserver does
before serving a URL. If the nsperm module is loaded, the algorithm is
as follows.
<pre>
1. If the authuser is "nsadmin", the password is correct, and the IP
address of the client is allowed nsadmin access, then access is
authorized.
2. Find the relevant permission record. If an exact match for the
method and URL combination is not found, the end of the URL is
pared down until a match is found. For example, if there is no
match for `/products/cereals/raisin_bran.html,' then the server
looks for a permission record for the URL `/products/cereals.' If
that permission record is specified as "Exact URL match is NOT
required", then that permission record is used.
</pre>
By default, the server comes with a row that says GET on `/' is open
to the world.
<p>
If no relevant permission record is found, access is denied
(forbidden).
<pre>
1. If the authuser is in the "Allow Users" list, access is permitted.
If the authuser is a member of a group in the "Allow Groups" list
and not in the "Deny Users' list, access is permitted.
2. If the host is in the "Hosts to allow" list, access is permitted.
If the host is in the "Hosts to deny" list, access is denied.
3. If the request does not come in with authorization data, access is
denied.
4. The user and password are verified. If there is no password
specified in the database, any password is accepted.
5. Otherwise, access is denied.
</pre>
<h3>Return Values</h3>
The following values can be returned by ns_checkurl.
<p>
OK<br>
The user has permission to execute this URL and method.<br>
DENIED<br>
The user does not have permission to execute this URL and method.<br>
FORBIDDEN<br>
There is no possible user/password/IP Address combination that would
give authorization.<br>
ERROR<br>
There was an error.
<p>
If the permissions module is not loaded, an error is returned.
<p>
<hr>
<br>
<h2><a href=./ name=ns_conn>ns_conn</a></h2>
Find information about the current HTTP connection.
<h3>Syntax</h3>
<table>
<tr>
<td valign=top align=left>
ns_conn authpassword<br>
ns_conn authuser<br>
ns_conn close<br>
ns_conn contentlength<br>
ns_conn driver<br>
ns_conn form<br>
ns_conn headers<br>
ns_conn host<br>
ns_conn isconnected<br>
ns_conn location<br>
ns_conn method<br>
</td>
<td valign=top align=left>
ns_conn outputheaders<br>
ns_conn peeraddr<br>
ns_conn port<br>
ns_conn protocol<br>
ns_conn query<br>
ns_conn request<br>
ns_conn url<br>
ns_conn urlc<br>
ns_conn urlv<br>
ns_conn version<br>
</td>
</tr>
</table>
<h3>Description</h3>
ns_conn authpassword returns the decoded user password from the
authorization data.
<p>
ns_conn authuser returns the decoded user name from the authorization
data.
<p>
ns_conn close closes the connection so the script (or ADP) can do any
time-consuming processing without making the client wait. If you use
ns_conn close in an ADP, streaming should be turned on before closing
the connection (i.e. <SCRIPT RUNAT=SERVER STREAM=ON>) or nothing
will get sent out at all.
<p>
ns_conn contentlength returns the number of bytes in the content
passed in.
<p>
ns_conn driver returns the name of the module (nssock or nsssl) that
is acting as the communications driver for this connection.
<p>
ns_conn form returns any submitted form data as an ns_set. This form
data may have been submitted with a POST or appended to the URL in a
GET request. Note: ns_conn form is not suitable for multipart
formdata file upload widgets.
<p>
ns_conn headers returns all the header data as an ns_set. The keys of
the ns_set represent the field names. The case of the returned field
names depends on the HeaderCase configuration parameter. By default,
HeaderCase is "Preserve", which means case is preserved.
<p>
ns_conn host returns the host part of the URL in the HTTP request.
<p>
ns_conn isconnected returns 1 if you're in a connection thread, and
you are therefore allowed to make calls to ns_conn. It returns 0 if
you're not in a connection thread (such as when you're in a schedule
procedure) and you are not allowed to make calls to ns_conn.
<p>
ns_conn location returns the location string for this virtual server
in the form: <tt>protocol://hostname[:port]</tt>.
<p>
ns_conn method returns the HTTP method, e.g. <tt>GET</tt>.
<p>
ns_conn outputheaders returns an ns_set containing the headers that
will be sent out when a result is returned to the client. This ns_set
can be manipulated like any other ns_set. You can also use this
command to write to the set of output headers. For example: <tt>ns_set
put [ns_conn outputheaders] key value</tt>.
<p>
ns_conn peeraddr returns the IP address of the client, i.e. the "other
side" of the HTTP connection. The IP address is returned in the form
of a string separated with periods (e.g., 155.164.59.75).
<p>
ns_conn port returns the port specified explicitly in the URL of the
HTTP request. If the browser does not explicity send the ":port" part
of the URL, the port number returned will be 0.
<p>
ns_conn protocol returns the protocol of the URL in the HTTP request
(usually unspecified).
<p>
ns_conn query returns any query data that was part of the HTTP
request.
<p>
ns_conn request returns the HTTP request line as presented by the
client, e.g. <tt>GET / HTTP/1.1</tt>.
<p>
ns_conn url returns the URL of the HTTP request. This is the portion
of the request after the hostname, for example <tt>[ns_conn url]</tt>
on http://aolserver.com/ returns <tt>/index.adp</tt>.
<p>
ns_conn urlc returns the number of elements (delimited by `/') in the
URL of the HTTP request.
<p>
ns_conn urlv returns a list containing the pieces of the URL delimited
by `/'.
<p>
ns_conn version returns the version of the HTTP request. This is
usually 1.0 or 1.1.
<p>
<hr>
<br>
<h2><a href=./ name=ns_conncptofp>ns_conncptofp</a></h2>
Write content to a file
<h3>Syntax</h3>
ns_conncptofp fileId
<h3>Description</h3>
This function writes all the content (including any embedded null
characters) to the specified file.
<p>
<hr>
<br>
<h2><a href=./ name=ns_connsendfp>ns_connsendfp</a></h2>
Write contents of file to conn
<h3>Syntax</h3>
ns_connsendfp fp len
<h3>Description</h3>
This function writes len bytes of the specified channel or file to the
conn.
<p>
<hr>
<br>
<h2><a href=./ name=ns_get_multipart_formdata>ns_get_multipart_formdata</a></h2>
Handle the POST action of a form containing one Netscape file widget
<h3>Syntax</h3>
ns_get_multipart_formdata key fieldId ?formdataSet?
<h3>Description</h3>
If you have a Tcl script that is handling a POST of a form containing
exactly one Netscape INPUT TYPE=FILE widget and 0 or more other
widgets, you can call ns_get_multipart_formdata instead of ns_conn
form. If you call ns_get_multipart_formdata at any other point, such
as inside an ADP script, it may not work properly due to AOLserver's
processing of the form data prior to execution of the ADP.
<p>
The key argument is the key for the file widget.
<p>
The fileId argument must be the file ID of a file that is open for
write operations. ns_get_multipart_formdata will write the submitted
file (from the file widget) to the file specified by fileId.
<p>
If you pass a formdataSet into the function, the rest of the form data
is dropped into that formdata set.
<h3>Example</h3>
In the HTML page, the form is defined as:
<pre>
<form enctype=multipart/form-data method=post action=/foo>
<input name=file type=file>
</form>
</pre>
The POST action is handled by the /foo script defined below:
<pre>
ns_register_proc POST /foo foo
proc foo {conn ignore} {
set fp [open "/tmp/uploaded_file" w+]
ns_get_multipart_formdata "file" $fp
close $fp
## Process file
## return something
}
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_getform>ns_getform</a></h2>
Return an ns_set that contains all of the query data that was part of
the HTTP request.
<h3>Syntax</h3>
ns_getform
<h3>Description</h3>
If there is query data (from either a form or multipart form data),
ns_getform returns an ns_set that can be queried for that data. If
there is no query data, "" is returned.
<p>
<hr>
<br>
<h2><a href=./ name=ns_geturl>ns_geturl</a></h2>
Fetch a URL.
<h3>Syntax</h3>
ns_geturl URL ?headersSetIdVar?
<h3>Description</h3>
This function retrieves the contents of the passed-in URL. If
headersSetIdVar is passed in and it is a valid ns_set, then the header
information received along with the request is inserted into it.
<h3>Example</h3>
<pre>
ns_register_proc GET /wais getwais
proc getwais { conn context } {
ns_return 200 text/html [ns_geturl http://www.wais.com/]
}
</pre>
<h3>Notes</h3>
This function should be used with caution. If the server is running
with 1 thread, and you perform ns_geturl back to the originating
server, the server may deadlock.
<p>
Also, ns_geturl does not follow redirects or handle relative URLs.
URLs that are server-relative (begin with "/") are translated into
filenames, and the content of the file is returned.
<p>
<hr>
<br>
<h2><a href=./ name=ns_hrefs>ns_hrefs</a></h2>
Return list of HTML <A> links.
<h3>Syntax</h3>
ns_hrefs HTML
<h3>Description</h3>
This function returns a Tcl list of all the URLs that the HTML
contains <A> links to.
<p>
<hr>
<br>
<h2><a href=./ name=ns_httptime>ns_httptime</a></h2>
Convert time in seconds to HTTP header format.
<h3>Syntax</h3>
ns_httptime time_in_seconds
<h3>Description</h3>
This function converts the time (specified as the number of seconds
from 00:00:00 UTC, January 1, 1970) to the appropriate format for an
HTTP header or log file. The time and date is returned with a four
digit year, for example: "Sun, 06 Nov 1994 08:49:37 GMT". You can use
the ns_time function to get the current time in seconds like this:
<pre>
set time [ns_httptime [ns_time]]
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_httpget>ns_httpget</a></h2>
Open an HTTP connection and fetch a page.
<h3>Syntax</h3>
ns_httpget url ?timeout? ?depth?
<h3>Description</h3>
ns_httpget opens an HTTP connection and fetches the page at the
specified url. You can specify a timeout for opening the connection
(the default is 30 seconds), and a maximum level of redirection (the
default is 0). ns_httpget sends the HTTP/1.1 "Host:" header for proper
support of virtual hosting but only supports HTTP/1.0.
<h3>Example</h3>
set page [ns_httpget http://www.aolserver.com]
<p>
<hr>
<br>
<h2><a href=./ name=ns_httpopen>ns_httpopen</a></h2>
Open an HTTP connection.
<h3>Syntax</h3>
ns_httpopen method url ?rqset? ?timeout?
<h3>Description</h3>
ns_httpopen opens an HTTP connection and performs the specified method
at the specified URL. The timeout is the number of seconds to wait for
the connection to open. ns_httpopen sends the HTTP/1.1 "Host:" header
for proper support of virtual hosting but only supports HTTP/1.0.
<p>
ns_httpopen returns a list with these three elements: a file
descriptor for reading, a file descriptor for writing, and a set ID
for a set describing the connection. The three elements of the set are
date, server, and content type.
<p>
<hr>
<br>
<h2><a href=./ name=ns_hostbyaddr>ns_hostbyaddr</a></h2>
Resolve an IP address to a hostname.
<h3>Syntax</h3>
ns_hostbyaddr ipaddress
<h3>Description</h3>
ns_hostbyaddr resolves the specified IP address to its corresponding
host name using AOLserver's built-in DNS cache. See the
Administration Guide for adjusting the DNS cache.
<p>
<hr>
<br>
<h2><a href=./ name=ns_parseheader>ns_parseheader</a></h2>
Parse HTTP header.
<h3>Syntax</h3>
ns_parseheader set header
<h3>Description</h3>
This function parses the HTTP header specified by header into an
ns_set specified by set.
<p>
<hr>
<br>
<h2><a href=./ name=ns_parsehttptime>ns_parsehttptime</a></h2>
Return number of seconds from HTTP time.
<h3>Syntax</h3>
ns_parsehttptime httptime
<h3>Description</h3>
ns_parsehttptime takes a properly formatted HTTP time and returns the
number of seconds since 00:00:00 UTC Jan 1, 1970.
<p>
<hr>
<br>
<h2><a href=./ name=ns_parsequery>ns_parsequery</a></h2>
Parse a query string.
<h3>Syntax</h3>
ns_parsequery querystring
<h3>Description</h3>
This function parses the specified querystring into an ns_set, which
is returned. The querystring takes the form: a=bcdefgh&b=123&c=rew.
<p>
<hr>
<br>
<h2><a href=./ name=ns_queryexists>ns_queryexists</a></h2>
Check for a key in the query data that was part of the HTTP request.
<h3>Syntax</h3>
ns_queryexists key
<h3>Description</h3>
ns_queryexists looks in the query data for the specified key. If the
key exists, 1 is returned; otherwise 0 is returned. The key is
interpreted in a case insensitive manner.
<h3>Example</h3>
<pre>
ns_register_proc POST /queryexiststest queryexiststest
proc queryexiststest { } {
if [ns_queryexists name] {
# ...process the form...
} else {
ns_returnerror 400 "you need to supply your name in the form"
}
} ;# queryexiststest
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_queryget>ns_queryget</a></h2>
Get a value from the query data that was part of the HTTP request.
<h3>Syntax</h3>
ns_queryget key ?value?
<h3>Description</h3>
ns_queryget looks in the query data for the specified key, and returns
the value that was included in the HTTP request. If the key does not
exist in the query data, "" is returned. The key is interpreted in a
case insensitive manner.
<p>
If the optional value argument is specified, and the key does not
exist in the query data, the specified value is simply returned. This
capability allows for providing a default value if the key doesn't
exist.
<p>
This function works for simple forms as well as for multipart
formdata.
<p>
For files uploaded with the Netscape file upload widget, the file that
was uploaded is an entry in the query data. See Example 3, below.
<h3>Examples</h3>
Example 1:
<pre>
set x [ns_queryget name]
</pre>
If "name" is a key in the query data, the variable x will be set to
the value associated with the "name" key. If "name" is not a key in
the query data, "" will be returned.
<p>
Example 2:
<pre>
set x [ns_queryget name Hoover]
</pre>
If "name" is a key in the query data, the variable x will be set to
the value associated with the "name" key. If "name" is not a key in
the query data, "Hoover" will be returned.
<p>
Example 3:
Given this HTML form:
<pre>
<form enctype=multipart/form-data method=POST
action=/formtest>
Local file: <input name=clientfile type=file>
To remote file: <INPUT TYPE=text NAME=path VALUE="" SIZE=80>
<input name=submit type=submit value=Upload>
</form>
</pre>
and this POST handler:
<pre>
proc formtest { } {
set remotefile [ns_queryget path]
set localfile [ns_queryget clientfile]
set where_the_data_is [ns_queryget clientfile.tmpfile]
} ;# formtest
</pre>
<p>
Suppose the user specified "spoon.txt" as the Local File and
"/oop/ack/tick.txt" as the Remote File, and then submitted the form.
The variable values in the formtest procedure will be:
remotefile = "/oop/ack/tick.txt"<br>
localfile = "spoon.txt"<br>
_the_data = something like: "/var/tmp/baaa29444"<br>
<p>
If you want to use the contents of the uploaded file, you can open it
by executing:
<pre>
open [ns_queryget clientfile.tmpfile]
</pre>
You can then read it and manipulate it as you want. Note, however,
that this tmp file will be deleted once the connection closes.
<p>
<hr>
<br>
<h2><a href=./ name=ns_querygetall>ns_querygetall</a></h2>
Get multiple query values.
<h3>Syntax</h3>
ns_querygetall key ?def_result?
<h3>Description</h3>
This function gets multiple query values whose key is key. If there
are none, the default result (def_result) is returned, or null is
returned if def_result is not specified. This function is useful for
checkboxes.
<p>
<hr>
<br>
<h2><a href=./ name=ns_requestauthorize>ns_requestauthorize</a></h2>
Ask the server to check permissions using nsperm.
<h3>Syntax</h3>
ns_requestauthorize method URL authuser authpassword ?ipaddr?
<h3>Description</h3>
This function does the same permission check that the AOLserver does
before serving a URL. If the nsperm module is loaded, the algorithm is
as follows.
<pre>
1. If the authuser is "nsadmin", the password is correct, and the IP
address of the client is allowed nsadmin access, then access is
authorized.
2. Find the relevant permission record. If an exact match for the
method and URL combination is not found, the end of the URL is
pared down until a match is found. For example, if there is no
match for `/products/cereals/raisin_bran.html,' then the server
looks for a permission record for the URL `/products/cereals.' If
that permission record is specified as "Exact URL match is NOT
required", then that permission record is used.
</pre>
By default, the server comes with a row that says GET on `/' is open
to the world.
<p>
If no relevant permission record is found, access is denied
(forbidden).
<pre>
1. If the authuser is in the "Allow Users" list, access is permitted.
If the authuser is a member of a group in the "Allow Groups" list
and not in the "Deny Users' list, access is permitted.
2. If the host is in the "Hosts to allow" list, access is permitted.
If the host is in the "Hosts to deny" list, access is denied.
3. If the request does not come in with authorization data, access is
denied.
4. The user and password are verified. If there is no password
specified in the database, any password is accepted.
5. Otherwise, access is denied.
</pre>
<h3>Return Values</h3>
The following values can be returned by ns_requestauthorize.
<p>
OK<br>
The user has permission to execute this URL and method.<br>
DENIED<br>
The user does not have permission to execute this URL and method.<br>
FORBIDDEN<br>
There is no possible user/password/IP Address combination that would
give authorization.<br>
ERROR<br>
There was an error.
<p>
<hr>
<br>
<h2><a href=./ name=ns_respond>ns_respond</a></h2>
Build a complete HTTP response.
<h3>Syntax</h3>
ns_respond ?-status status? ?-type type? {?-string string? | ?-file file? | ?-fileid fileid? } ?-length length? ?-headers setId?
<h3>Description</h3>
ns_respond builds a complete response for the client with all of the
specified information in the header.
<h3>Example</h3>
Using ns_respond, it's easy to do an HTTP redirect:
<pre>
set headers [ns_set new myheaders]
ns_set put $headers location http://www.aolserver.com
ns_respond -status 302 -type text/plain \
-string "redirection" -headers $headers
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_return>ns_return</a></h2>
Return the response to the client.
<h3>Syntax</h3>
ns_return status type string<br>
ns_returnadminnotice status msg ?longmsg?<br>
ns_returnbadrequest reason<br>
ns_returnerror status msg<br>
ns_returnfile status type filename<br>
ns_returnforbidden<br>
ns_returnfp status type fileId len<br>
ns_returnnotfound<br>
ns_returnnotice status msg ?longmsg?<br>
ns_returnredirect location<br>
ns_returnunauthorized<br>
<h3>Description</h3>
These procedures provide a simple interface for returning information
to the client. They build HTTP/1.0 headers and send the appropriate
data out the socket to the client. The script does not end at the
time ns_return* is invoked so you can continue processing data after
the client has gotten the data and closed the socket.
<p>
type should be a MIME type (see ns_guesstype manual page for a list).<br>
status is a three-digit number fitting the pattern below:
<blockquote>
1xx Informational - Not used, but reserved for future use.<br>
2xx Success - The action was successfully received, understood, and
accepted.<br>
3xx Redirection - Further action must be taken in order to complete the
request.<br>
4xx Client Error - The request contains bad syntax or cannot be fulfilled.<br>
5xx Server Error - The server failed to fulfill an apparently valid
request.
</blockquote>
Some common status values and their meanings are:
<blockquote>
<table>
<tr>
<td nowrap align=left valign=top
200 OK<br>
201 Created<br>
202 Accepted<br>
203 Provisional Information<br>
204 No Content<br>
300 Multiple Choices<br>
301 Moved Permanently<br>
302 Moved Temporarily<br>
303 Method<br>
304 Not Modified<br>
</td>
<td nowrap align=left valign=top
400 Bad Request<br>
401 Unauthorized<br>
402 Payment Required<br>
403 Forbidden<br>
404 Not Found<br>
405 Method Not Allowed<br>
406 None Acceptable<br>
407 Proxy Authentication Required<br>
408 Request Time-out<br>
409 Conflict<br>
410 Gone<br>
</td>
<td nowrap align=left valign=top
500 Internal Server Error<br>
501 Not Implemented<br>
502 Bad Gateway<br>
503 Service Unavailable<br>
504 Gateway Time-out<br>
</td>
</tr>
</table>
</blockquote>
<p>
ns_return sends back both the headers and the string.
<p>
ns_returnadminnotice performs the same function as ns_returnnotice,
except that it appends a line with a link to
"mailto:serveradministrator" based on the virtual server parameter
"WebMaster".
<p>
ns_returnbadrequest returns a 400 status code and a formatted HTML
message containing the reason text.
<p>
ns_returnerror wraps the text msg in some html and returns that to the
client.
<p>
ns_returnfile sends back the headers and the contents of the file.
<p>
ns_returnforbidden returns a 403 status code.
<p>
ns_returnfp first sends the appropriate headers. Next, it writes out
the contents of file from the current file position until the end of
the file.
<p>
ns_returnnotfound returns a 404 status code.
<p>
ns_returnnotice wraps the text msg and longmsg in some html and
returns that to the client.
<p>
ns_returnredirect returns a redirect to the passed in location.
<p>
ns_returnunauthorized returns a 401 status code.
<h3>Example</h3>
It's often handy to output a page to the user and close the connection
but still process data afterwards. Your script will continue to run
after you invoke an "ns_return*" function.
<pre>
ns_returnnotice 200 "Thank you" "Thank you for your input!"
# Do more stuff...
# Maybe get a cup of coffee...
# It doesn't matter since the client has already left.
return TCL_OK ;# not really needed but shown here for clarity.
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_setexpires>ns_setexpires</a></h2>
Set the HTTP "Expires" header.
<h3>Syntax</h3>
ns_setexpires seconds
<h3>Description</h3>
This function sets the Expires header so the page will expire in the
specified number of seconds. Setting a negative value typically
causes broken images on browsers so use this option with caution.
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockaccept>ns_sockaccept</a></h2>
Accept a new socket connection.
<h3>Syntax</h3>
ns_sockaccept sockid
<h3>Description</h3>
ns_sockaccept accepts a new connection pending on sockid.
<h3>Example</h3>
<pre>
#listen for connections on port 9000
set sock [ns_socklisten * 9000]
#wait for new connection
set fds [ns_sockaccept $sock]
set rfd [lindex $fds 0]
set wfd [lindex $fds 1]
puts $wfd "Hello!"
close $rfd
close $wfd
close $sock
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockblocking>ns_sockblocking</a></h2>
Set the blocking option on the socket.
<h3>Syntax</h3>
ns_sockblocking sockId
<h3>Description</h3>
This function sets the socket for the specified sockId to "blocking."
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockcallback>ns_sockcallback</a></h2>
Register a socket callback script.
<h3>Syntax</h3>
ns_sockcallback sockid script when
<h3>Description</h3>
ns_sockcallback registers a socket callback script. The script should
accept the arguments sockid and when. The script will be called
according to the value of the "when" argument as follows:
<p>
r = the socket is readable<br>
w = the socket is writeable<br>
e = the socket has an exceptional condition<br>
x = the server is shutting down<br>
<h3>Example</h3>
<pre>
set sock [ns_socklisten * 9000]
ns_sockcallback $sock newconn r
# Keep $sock from closing after connection ends
detach $sock
# When a connection arrives, newconn will be called as:
# newconn $sock r
proc newconn {sock when} {
set fds [ns_sockaccept $sock]
set rfd [lindex $fds 0]
set wfd [lindex $fds 1]
puts $wfd "Hello!"
close $rfd
close $wfd
}
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockcheck>ns_sockcheck</a></h2>
Check if a socket is connected.
<h3>Syntax</h3>
ns_sockcheck fileid
<h3>Description</h3>
ns_sockcheck uses recv() or send() calls to check if a socket is still
connected. The fileid is the read or write file id returned by
ns_sockopen. This function is useful if you used the -nonblock option
with ns_sockopen after calling ns_sockselect.
<h3>Example</h3>
An example containing ns_sockcheck is provided under ns_sockselect.
<p>
<hr>
<br>
<h2><a href=./ name=ns_socketpair>ns_socketpair</a></h2>
Create a pair of connected sockets.
<h3>Syntax</h3>
ns_socketpair
<h3>Description</h3>
ns_socketpair creates a pair of connected sockets and returns a list
of file ids: the first one for reads and the second one for writes.
<p>
<hr>
<br>
<h2><a href=./ name=ns_socklisten>ns_socklisten</a></h2>
Create a new socket listening for connections.
<h3>Syntax</h3>
ns_socklisten address port
<h3>Description</h3>
ns_socklisten creates a new socket listening for connections at the
specified interface address and port. "*" can be used as the address
argument to specify all addresses.
<p>
<hr>
<br>
<h2><a href=./ name=ns_socklistencallback>ns_socklistencallback</a></h2>
Run script on connection.
<h3>Syntax</h3>
ns_socklistencallback address port script
<h3>Description</h3>
This function listens on the specified address and port and runs the
specified script when new connections are received.
<p>
<hr>
<br>
<h2><a href=./ name=ns_socknonblocking>ns_socknonblocking</a></h2>
Set nonblocking option on the socket.
<h3>Syntax</h3>
ns_socknonblocking sockId
<h3>Description</h3>
This function sets the socket option for the specified sockId to
"nonblocking."
<p>
<hr>
<br>
<h2><a href=./ name=ns_socknread>ns_socknread</a></h2>
Return bytes to be read.
<h3>Syntax</h3>
ns_socknread sockId
<h3>Description</h3>
This function returns the number of bytes waiting to be read from the
specified sockId.
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockopen>ns_sockopen</a></h2>
Connect to a remote host on the specified port.
<h3>Syntax</h3>
ns_sockopen ?-nonblock | -timeout seconds? host port
<h3>Description</h3>
ns_sockopen uses socket(), bind(), and connect() calls to connect to
the remote host (host) on the specified port (port). ns_sockopen
returns a list of two file ids: the first one for reads and the second
one for writes.
<p>
The -nonblock option specifies that ns_sockopen will return
immediately, while the connect is still in progress by the operating
system. If you use -nonblock, you then can call ns_sockcheck with the
write file id to see if the connect was actually completed.
<p>
The -timeout option specifies how long to wait for the connect to be
made in seconds before timing out.
<h3>Example</h3>
This is a simple example that fetches a page from an http server
(www.aolserver.com) on port 80. The headers will be in the $headers
list, and the body will be in the $page list.
<pre>
set fds [ns_sockopen www.aolserver.com 80]
set rid [lindex $fds 0]
set wid [lindex $fds 1]
puts $wid "GET /index.htm HTTP/1.0\r\n\r"
flush $wid
while {[set line [string trim [gets $rid]]] != ""} {
lappend headers $line
}
set page [read $rid]
close $rid
close $wid
</pre>
A more advanced example containing ns_sockopen is provided under
ns_sockselect.
<p>
<hr>
<br>
<h2><a href=./ name=ns_sockselect>ns_sockselect</a></h2>
Determine readable file id's.
<h3>Syntax</h3>
ns_sockselect ?-timeout seconds? rfds wfds efds
<h3>Description</h3>
ns_sockselect uses a select() call to determine which file id's re
readable, writeable, or have exceptional conditions. ns_sockselect
returns a list of three lists of: readable file id's, writeable file
id's, and file id's with exceptions.
<p>
The -timeout option specifies the length of time to wait in seconds
for ns_sockselect to return before timing out.
<p>
The rfds, wfds, and efds arguments are lists of readable file id's,
writeable file id's, and file id's with exceptions, respectively.
<h3>Example</h3>
This example attempts to connect to nine servers at once and service
the first available connections:
<pre>
# Start nonblocking connect()'s to www01 through
# www09.foo.com and remember the read fileid which
# corresponds to each write fileid.
for {set n 1} {$n < 10} {incr n} {
set host [format "www%2d.foo.com" $n]
set fds [ns_sockopen -nonblock $host 80]
set r [lindex $fds 0]
set w [lindex $fds 1]
set w2r($w) $r
lappend wfds $w
}
# All connect()'s are in progress, use select to wait for one or
# more to become writable in the next two seconds which means #
# they may have connected. We're not interested in readable or
# exception sockets so the corresponding lists are empty
# (i.e., {}).
set sel [ns_sockselect -timeout 2 {} $wfds {}]
# Select returned - get the sockets ready to write to.
set wfds [lindex $sel 1]
# Use ns_sockcheck to see if the sockets actually connected and
# didn't become writable because the connect() failed (e.g., no
# Web server was running on port 80 on the corresponding machine).
# Note that the wfds list may be empty, meaning all sockets timed
# out on connect.
set ok ""
foreach w $wfds {
if [ns_sockcheck $w] {
# Socket is connected - send a GET HTTP request.
lappend ok $w
puts $w "GET /index.htm HTTP/1.0\r\n\r"
# The flush is important, otherwise the remote
# server may never see the data.
flush $w
}
}
# Get the read ids for each socket which we sent the GET request to.
foreach w $ok {
lappend rfds $w2r($w)
}
# Use select again to wait for the read sockets to have data
# available in response to the GET request.
set sel [ns_sockselect -timeout 2 $rfds {} {}]
set rfds [lindex $sel 0]
# Read the pages which came back.
foreach r $rfds {
if [ns_sockcheck $r] {
set page($r) [read $r]
}
}
# Close all the sockets
foreach w [array names w2r] {
close $w
close $w2r($w)
}
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_url2file>ns_url2file</a></h2>
Return file pathname that corresponds to the URL.
<h3>Syntax</h3>
ns_url2file URL
<h3>Description</h3>
This function returns the file or directory corresponding to the
specified URL. The file returned is located in the "pages" directory
on the current virtual server.
<h3>Example</h3>
This is especially useful when using the ns_adp_include command.
<pre>
<% ns_adp_include [ns_url2file /include/myfile.inc] %>
</pre>
<p>
<hr>
<br>
<h2><a href=./ name=ns_write>ns_write</a></h2>
Write raw content back to the client.
<h3>Syntax</h3>
ns_write string
<h3>Description</h3>
ns_write writes the string out the connection. You can use it instead
of the ns_return or ns_respond functions to build HTTP responses.
AOLserver will not include the usual headers on the output data. The
script does not end at the time ns_write* is invoked so you can
continue processing data after the client has gotten the data and
closed the socket.
<h3>Example</h3>
See ns_return.
<p>
<hr>
<br>
<h2><a href=./ name=ns_writecontent>ns_writecontent</a></h2>
Write content to a file.
<h3>Syntax</h3>
ns_writecontent fileId
<h3>Description</h3>
This function writes all the content (including any embedded null
characters) to the specified file.
<p>
<hr>
<br>
</body>
</html>
|