ディスパッチ

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

ディスパッチ

web2pyは次の形式のURLを:

web2py maps a URL of the form:

http://127.0.0.1:8000/a/c/f.html

"a"というアプリケーションの"c.py"というコントローラのf()関数に割り当てます。もしfが提示されていない場合、indexというコントローラの関数がデフォルトとして使用されます。cが提示されていない場合は、"default.py"がコントローラがデフォルトとして使用されます。aが提示されていない場合は、initというアプリケーションがデフォルトとして使用されます。initというアプリケーションがない場合、web2pyはwelcomeというアプリケーションの実行を試みます。下の画像は、この内容を模式的に示したものです:

to the function f() in controller "c.py" in application "a". If f is not present, web2py defaults to the index controller function. If c is not present, web2py defaults to the "default.py" controller, and if a is not present, web2py defaults to the init application. If there is no initapplication, web2py tries to run the welcome application. This is shown schematically in the image below:

(デフォルトのアプリケーション、コントローラ、関数の名前は、routes.pyにて書き換えることができます; 後述のデフォルトアプリケーション、コントローラ、関数 を参照してください)

(The names of the default application, controller and function can be overridden in routes.py; see Default Application, Controller and Function below.

デフォルトでは、任意の新しいリクエストはまた、新しいセッションを作成します。加えて、セッション・クッキーは、セッションを維持するためにクライアントのブラウザに返されます。

By default, any new request also creates a new session. In addition, a session cookie is returned to the client browser to keep track of the session.

拡張子.htmlは省略できます。その場合.htmlがデフォルトとして仮定されます。この拡張子はビューの拡張子を決定します。ここでビューはコントローラ関数f()の出力をレンダリングするものです。拡張子によって、同じコンテンツを複数の形式(HTML、XML、JSON、RSSフィードなど)で提供することができます。

The extension .html is optional; .html is assumed as default. The extension determines the extension of the view that renders the output of the controller function f(). It allows the same content to be served in multiple formats (html, xml, json, rss, etc.).

引数をとる関数や、2つのアンダースコアで始まる関数は一般には公開されず、他の関数からしか呼び出すことができません。

Functions that take arguments or start with a double underscore are not publicly exposed and can only be called by other functions.

URLの形式では、次のような例外があります。

There is an exception made for URLs of the form:

http://127.0.0.1:8000/a/static/filename

ここで、"static"というコントローラは存在しません。web2pyはこれを、"a"というアプリケーションの"static"というサブフォルダにある"filename"という名前のファイルへのリクエストとして解釈します。

There is no controller called "static". web2py interprets this as a request for the file called "filename" in the subfolder "static" of the application "a".

静的ファイルをダウンロードするとき、web2pyはセッションを作成せず、クッキーを発行したりモデルを実行することもありません。web2pyは常に静的ファイルを1MBごとに分割して、PARTIAL CONTENT を送信します。このとき、クライアントは分割ファイルに対してRANGEリクエストを送ることになります。

When static files are downloaded, web2py does not create a session, nor does it issue a cookie or execute the models. web2py always streams static files in chunks of 1MB, and sends PARTIAL CONTENT when the client sends a RANGE request for a subset of the file.

web2pyはまた、IF_MODIFIED_SINCEプロトコルもサポートしています。ファイルがすでにブラウザに保存されていて、そのファイルが現在のバージョンから変更されていない場合、ファイルは送信されません。

web2py also supports the IF_MODIFIED_SINCE protocol, and does not send the file if it is already stored in the browser's cache and if the file has not changed since that version.

web2pyは、次のような形式のGET/POSTのリクエストを:

web2py maps GET/POST requests of the form:

http://127.0.0.1:8000/a/c/f.html/x/y/z?p=1&q=2

aアプリケーションの"c.py"というコントローラの関数fに割り当て、URLのパラメータをrequest変数に次のように保存します:

to function f in controller "c.py" in application a, and it stores the URL parameters in therequest variable as follows:

1.

request.args = ['x', 'y', 'z']

および:

and:

1.

request.vars = {'p':1, 'q':2}

および:

and:

1.

2.

3.

request.application = 'a'

request.controller = 'c'

request.function = 'f'

上記の例では、request.args[i]とrequest.args(i)はいずれも、request.argsのi番目の要素を取得します。しかし前者は、request.argsにi番目の要素がない場合に例外が発生します。後者はNoneを返します。

In the above example, both request.args[i] and request.args(i) can be used to retrieve the i-th element of the request.args, but while the former raises an exception if the list does not have such an index, the latter returns None in this case.

1.

これは、現在のリクエストに対する完全なURL(ただしGET変数は含まれない)を格納します。

stores the full URL of the current request (not including GET variables).

1.

request.ajax

これはデフォルトでFalseですが、Ajaxリクエストでアクションが呼ばれたとweb2pyが判断した場合にはTrueになります。

defaults False but it is True if web2py determines that the action was called by an Ajax request.

リクエストがAjaxで、web2pyコンポーネントによって引き起こされたものであれば、そのコンポーネントの名前は次のものから知ることができます:

If the request is an Ajax request and it is initiated by a web2py component, the name of the component can be found in:

1.

コンポーネントについては、第13章で詳しく説明します。

Components are discussed in more detail in Chapter 13.

HTTPリクエストがGETである場合、request.env.request_methodには"GET"が設定されます。POSTの場合、request.env.request_methodには"POST"が設定されます。URLのクエリ変数はrequest.varsのStorage辞書として格納されます。それらの値はrequest.get_vars(GETリクエストの場合)または、request.post_vars(POSTリクエストの場合)にも格納されます。

If the HTTP request is a GET, then request.env.request_method is set to "GET"; if it is a POST, request.env.request_method is set to "POST". URL query variables are stored in therequest.vars Storage dictionary; they are also stored in request.get_vars (following a GET request) or request.post_vars (following a POST request).

WSGIとweb2pyの環境変数はrequest.envに、次の例のように格納されます:

web2py stores WSGI and web2py environment variables in request.env, for example:

1.

request.env.path_info = 'a/c/f'

また、HTTPヘッダを環境変数に次のように格納します:

and HTTP headers into environment variables, for example:

1.

request.env.http_host = '127.0.0.1:8000'

web2pyはすべてのURLに対して、ディレクトリトラバーサル攻撃を防ぐために検証を行っています。

Notice that web2py validates all URLs to prevent directory traversal attacks.

URLは、英数字、アンダースコア、スラッシュのみでしか構成されません。ただし、argsは連続しないドットを含むこともあります。空白文字は、検証の前にアンダースコアに置き換えられます。URL構文が無効な場合は、HTTP 400エラーメッセージを返します。

URLs are only allowed to contain alphanumeric characters, underscores, slashes; the args may contain non-consecutive dots. Spaces are replaced by underscores before validation. If the URL syntax is invalid, web2py returns an HTTP 400 error message .

URLが静的ファイルへのリクエストに一致する場合、web2pyは要求されたファイルを単純に読み込んで返します(ストリーミングします)。

If the URL corresponds to a request for a static file, web2py simply reads and returns (streams) the requested file.

URLが静的ファイルへのリクエストでなかった場合、web2pyはリクエストを次の順序で処理します:

If the URL does not request a static file web2py processes the request in the following order:

  • クッキーを解析します。

    • Parses cookies.

  • 関数が実行される環境を作成します。

    • Creates an environment in which to execute the function.

  • request、response、cacheを初期化します。

    • Initializes request, response, cache.

  • 既存のsessionを開くか、新しいものを作成します。

    • Opens the existing session or creates a new one.

  • リクエストされたアプリケーションに属するモデルを実行します。

    • Executes the models belonging to the requested application.

  • リクエストされたコントローラのアクション関数を実行します。

    • Executes the requested controller action function.

  • 関数が辞書を返す場合、関連付けられたビューを実行します。

    • If the function returns a dictionary, executes the associated view.

  • 成功した場合、すべての開いているトランザクションをコミットします。

    • On success, commits all open transactions.

  • セッションを保存します。

    • Saves the session.

  • HTTPレスポンスを返します。

    • Returns an HTTP response.

コントローラとビューは同じ環境の異なるコピーで実行されることに注意してください。したがって、ビューはコントローラを参照しません。代わりに、モデル、および、コントローラのアクション関数によって返された変数を、参照します。

Notice that the controller and the view are executed in different copies of the same environment; therefore, the view does not see the controller, but it sees the models and it sees the variables returned by the controller action function.

(HTTP以外の)例外が発生した場合、web2pyは次の処理を行います:

If an exception (other than HTTP) is raised, web2py does the following:

  • トレースバックをエラーファイルに格納し、チケット番号をそれに割り当てます。

    • Stores the traceback in an error file and assigns a ticket number to it.

  • すべての開いているトランザクションをロールバックします。

    • Rolls back all open transactions.

  • チケット番号を載せたエラーページを返します。

    • Returns an error page reporting the ticket number.

例外がHTTP例外の場合、意図した動作(たとえばHTTPのリダイレクト)と見なされ、すべての開いているデータベーストランザクションがコミットされます。その後の挙動は、HTTP例外自身によって定められます。HTTP例外クラスは、Python標準の例外ではなく、web2pyで定義されています。

If the exception is an HTTP exception, this is assumed to be the intended behavior (for example, an HTTP redirect), and all open database transactions are committed. The behavior after that is specified by the HTTP exception itself. The HTTP exception class is not a standard Python exception; it is defined by web2py.