Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Last active May 7, 2020 22:21
Show Gist options
  • Save jamiehodge/4458055 to your computer and use it in GitHub Desktop.
Save jamiehodge/4458055 to your computer and use it in GitHub Desktop.
Webmachine state machine and resource callbacks

Webmachine State Machine

General

  • Service available?
    • callback: service_available?
    • false: 503 Service Unavailable
  • Known method?
    • callback: known_methods
    • absent: 501 Not Implemented
  • URI too long?
    • callback: uri_too_long?
    • true: 414 Request URI Too Long
  • Method allowed?
    • callback: allowed_methods
    • absent: 405 Method Not Allowed
  • Malformed request?
    • callbacks: validate_content_checksum, malformed_request?
    • true: 400 Bad Request
  • Authorized?
    • callback: is_authorized?
    • false: 401 Unauthorized
  • Forbidden?
    • callback: forbidden?
    • true: 403 Forbidden
  • Valid Content-* headers?
    • callback: valid_content_headers?
    • false: 501 Not Implemented
  • Known content type?
    • callback: known_content_type?
    • false: 415 Unsupported Media Type
  • Request entity too large?
    • callback: valid_entity_length?
    • false: 413 Request Entity Too Large
  • OPTIONS?
    • callback: options
    • true: 200 OK

Content Negotiation

  • Accept exists?
    • true: Acceptable media type available?
      • callback: content_types_provided
      • absent: 406 Not Acceptable
  • Accept-Language exists?
    • true: Acceptable language available?
      • callback: languages_provided
      • absent: 406 Not Acceptable
  • Accept-Charset exists?
    • true: Acceptable character set available?
      • callback: charsets_provided
      • absent: 406 Not Acceptable
  • Accept-Encoding exists?
    • true: Acceptable encoding available?
      • callback: encodings_available
      • absent: 406 Not Acceptable
  • Set Content-Type header
  • Set Vary Header

Resource Exists?

  • Resource exists?
    • callback: resource_exists?

Exists

Conditional Request

  • If-Match exists?
    • true: If-Match: * exists?
      • false: ETag in If-Match?
        • callback: generate_etag
        • false: 412 Precondition Failed
  • If-Unmodified-Since exists?
    • true: If-Unmodified-Since is valid date?
      • true: Last-Modified > If-Modified-Since?
        • callback: last_modified
        • true: 412 Precondition Failed
  • If-None-Match exists?
    • true: If-None-Match: * exists?
      • false: ETag in If-None-Match?
        • callback: generate_etag
        • true: 304 Not Modified
        • false: 412 Precondition Failed
      • true: GET/HEAD?
        • true: 304 Not Modified (efficient cache updates)
        • false: 412 Precondition Failed (avoid PUT race conditions)
  • If-Modified-Since exists?
    • true: If-Modified-Since is valid date?
      • true: If-Modified-Since > now?
        • false: Last-Modified > If-Modified-Since?
          • callback: last_modified
          • false: 304 Not Modified

Verb

  • DELETE?

    • true: Delete
      • callback: delete_resource
      • false: 500 Internal Server Error
      • true: Delete enacted?
        • callbacks: delete_completed?
        • false: 202 Accepted
        • true: Response includes an entity?
          • false: 204 No Content
          • true: Multiple Choices?
            • callback: multiple_choices?
            • false: 200 OK
            • true: 300 Multiple Choices
  • POST?

    • true: Redirect?
      • callback: post_is_create?
      • true: Create
        • callbacks: base_uri, create_path, content_types_accepted -> handler
        • true: 303 See Other
      • false: Process
        • callbacks: process_post
        • true: New Resource?
          • true: 201 Created
          • false: Response includes an entity?
            • false: 204 No Content
            • true: Multiple Choices?
              • callback: multiple_choices?
              • false: 200 OK
              • true: 300 Multiple Choices
  • PUT?

    • true: Conflict?
      • callback: is_conflict?
      • true: 409 Conflict
      • false: New Resource?
        • true: 201 Created
        • false: Response includes an entity?
          • false: 204 No Content
          • true: Multiple Choices?
            • callback: multiple_choices?
            • false: 200 OK
            • true: 300 Multiple Choices
  • (GET/HEAD)

    • Generate body
      • callback: content_types_provided -> handler
    • Multiple Choices?
      • callback: multiple_choices?
      • false: 200 OK
      • true: 300 Multiple Choices

Does not exist

  • If-Match: * Exists?
    • true: 412 Precondition Failed (PUT race conditions)
    • false: PUT?

PUT

  • Apply PUT to a different URL?
    • callback: moved_permanently?
    • true: 301 Moved Permanently
    • false: Conflict?
      • callback: is_conflict?
      • true: 409 Conflict
      • false: New Resource?
        • true: 201 Created
        • false: Response includes an entity?
          • false: 204 No Content
          • true: Multiple Choices?
            • callback: multiple_choices?
              • false: 200 OK
              • true: 300 Multiple Choices

GET/HEAD/POST

  • Resource previously existed?
    • callback: previously_existed?
Existed
  • Resource moved permanently?
    • callback: moved_permanently?
    • true: 301 Moved Permanently
  • Resource moved temporarily?
    • callback: moved_temporarily?
    • true: 307 Moved Temporarily
Never existed
  • POST?
    • false: 404 Not Found
    • true: Permit POST to missing resource?
      • callback: allow_missing_post?
      • false: 404 Not Found
      • true: Redirect?
        • callback: post_is_create?
        • true: Create
          • callbacks: base_uri, create_path, content_types_accepted -> handler
          • true: 303 See Other
        • false: Process
          • callbacks: process_post
          • true: New Resource?
            • true: 201 Created
            • false: Response includes an entity?
              • false: 204 No Content
              • true: Multiple Choices?
                • callback: multiple_choices?
                • false: 200 OK
                • true: 300 Multiple Choices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment