Skip to content

Commit

Permalink
feat: import
Browse files Browse the repository at this point in the history
  • Loading branch information
Asaf Shemesh committed Dec 6, 2024
0 parents commit f833ef9
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

*.log

# virtualenv
.venv
venv/
ENV/
.vscode
.idea/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# pydantic mod-wsgi bug

This repository is a minimal reproduction of a bug that occurs when using some pydantic features with mod-wsgi.

This was tested on a Windows machine, with `mod-wsgi==4.9.2`, Python 3.9.9, apache 2.4.57.0

Also tested with Python 3.11 and bug still exists. Tried also `mod_wsgi==5.0.2`.

## Installation
- Get a Windows apache release from https://www.apachelounge.com/download/
- Place your installation at C:/Apache24

Then, install mod-wsgi (Requires VS build tools):
```bash
pip install mod-wsgi==4.9.2
```

```bash
pip install -r requirements.txt
```

### Configure apache
- Open `apache24-django.conf` and replace APP_ROOT to the absolute path of the wsgi.py folder, e.g:
`DEFINE WSGI_ROOT "C:/your_workspace/pydantic_mod_wsgi_bug/pydantic_mod_wsgi_bug"`
- If you don't use python 3.9 run `mod_wsgi-express module-config` and replace the three lines in `apache24-django.conf` with the output of the command.

- Start apache server

## Run
Try the endpoints with django dev server, where they will all work:
`python manage.py runserver`
- Browse to `http://localhost:8000/docs`


Then, try the endpoints with apache server, where some of the endpoints will fail:
- Browser to `http://localhost/docs`
The endpoints with string strip and string constraints, will hang, and will actually cause apache to crash.
- (Check apache error.log, you will see the server has crashed and restarted)
61 changes: 61 additions & 0 deletions apache24-django.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
###############################
# For Apache 2.4.x
###############################


## Output from mod_wsgi-express module-config:
# If you don't use python3.9, replace with the output you got from running 'mod_wsgi-express module-config'
LoadFile "C:/Python39/python39.dll"
LoadModule wsgi_module "C:/Python39/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
WSGIPythonHome "C:/Python39"
## end of output from mod_wsgi-express module-config


WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On


# This reduce the default 'Server' header sent by apache, to only send Server: Apache
ServerName localhost
ServerTokens ProductOnly
ServerSignature Off


# Replace here: Set this variable to the folder where wsgi.py is located
DEFINE WSGI_ROOT "C:/Asaf/pydantic_mod_wsgi_bug/pydantic_mod_wsgi_bug"

<IfModule alias_module>
<Directory "${WSGI_ROOT}">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIScriptAlias / "${WSGI_ROOT}/wsgi.py"
</IfModule>


# Compression of file types, for faster download. requires mod_deflate
<IfModule deflate_module>
AddOutputFilterByType DEFLATE text/plain text/html text/css application/javascript application/x-javascript
</IfModule>



LoadModule evasive_module modules/mod_evasive.so
<IfModule evasive_module>
DOSEnabled true
DOSHashTableSize 3097
DOSPageCount 4
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
</IfModule>


<VirtualHost *:80>
RewriteEngine off
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
Empty file added app/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AppConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "app"
Empty file added app/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
42 changes: 42 additions & 0 deletions app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Annotated

from ninja import NinjaAPI, Form
from pydantic import ConfigDict, BaseModel, StringConstraints

router = NinjaAPI()


class PayloadString(BaseModel):
# This works with apache
string: str


class PayloadStringStrip(BaseModel):
# This will fail with apache
string: str

model_config = ConfigDict(str_strip_whitespace=True)


class PayLoadStringConstraints(BaseModel):
number: Annotated[str, StringConstraints(pattern=r"^0\d\d$", strip_whitespace=True)]



@router.post("/payload_string")
def payload_string(request, payload: Form[PayloadString]):
# Works with no issues
return {"status": "OK", "payload": payload.model_dump()}


@router.post("/payload_string_strip")
def payload_string_strip(request, payload: Form[PayloadStringStrip]):
# Won't work in apache
return {"status": "OK", "payload": payload.model_dump()}



@router.post("/payload_string_constraints")
def payload_string_constraints(request, payload: Form[PayLoadStringConstraints]):
# This works only if you input a non-valid number, e.g '11'. For a valid input like '123', apache will freeze
return {"status": "OK", "payload": payload.model_dump()}
Empty file added db.sqlite3
Empty file.
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == "__main__":
main()
Empty file.
16 changes: 16 additions & 0 deletions pydantic_mod_wsgi_bug/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for pydantic_mod_wsgi_bug project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings")

application = get_asgi_application()
123 changes: 123 additions & 0 deletions pydantic_mod_wsgi_bug/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Django settings for pydantic_mod_wsgi_bug project.
Generated by 'django-admin startproject' using Django 4.1.12.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-*yu4s3b=@y^&2$hlm#(xmt4h4+k3kvx$lb(4w&&gdp7gt^1=dj"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "pydantic_mod_wsgi_bug.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "pydantic_mod_wsgi_bug.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
24 changes: 24 additions & 0 deletions pydantic_mod_wsgi_bug/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""pydantic_mod_wsgi_bug URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path

from app import views

urlpatterns = [
path("", views.router.urls),
]
18 changes: 18 additions & 0 deletions pydantic_mod_wsgi_bug/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
WSGI config for pydantic_mod_wsgi_bug project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

import os
import sys

from django.core.wsgi import get_wsgi_application

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pydantic_mod_wsgi_bug.settings")

application = get_wsgi_application()
Binary file added requirements.txt
Binary file not shown.

0 comments on commit f833ef9

Please sign in to comment.