##// END OF EJS Templates
hgweb: use a capped reader for WSGI input stream...
hgweb: use a capped reader for WSGI input stream Per PEP 3333, the input stream from WSGI should respect EOF and prevent reads past the end of the request body. However, not all WSGI servers guarantee this. Notably, our BaseHTTPServer based built-in HTTP server doesn't. Instead, it exposes the raw socket and you can read() from it all you want, getting the connection in a bad state by doing so. We have a "cappedreader" utility class that proxies a file object and prevents reading past a limit. This commit converts the WSGI input stream into a capped reader when the input length is advertised via Content-Length headers. "cappedreader" only exposes a read() method. PEP 3333 states that the input stream MUST also support readline(), readlines(hint), and __iter__(). However, since our WSGI application code only calls read() and since we're not manipulating the stream exposed by the WSGI server, we're not violating the spec here. Differential Revision: https://phab.mercurial-scm.org/D2768

File last commit:

r36074:7f8338b8 default
r36870:290fc4c3 default
Show More
diffhelpers.c
194 lines | 4.8 KiB | text/x-c | CLexer
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 /*
* diffhelpers.c - helper routines for mpatch
*
* Copyright 2007 Chris Mason <chris.mason@oracle.com>
*
* This software may be used and distributed according to the terms
* of the GNU General Public License v2, incorporated herein by reference.
*/
#include <Python.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
static char diffhelpers_doc[] = "Efficient diff parsing";
static PyObject *diffhelpers_Error;
/* fixup the last lines of a and b when the patch has no newline at eof */
static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b)
{
Py_ssize_t hunksz = PyList_Size(hunk);
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyObject *s = PyList_GET_ITEM(hunk, hunksz - 1);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 char *l = PyBytes_AsString(s);
Py_ssize_t alen = PyList_Size(a);
Py_ssize_t blen = PyList_Size(b);
char c = l[0];
PyObject *hline;
Py_ssize_t sz = PyBytes_GET_SIZE(s);
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 if (sz > 1 && l[sz - 2] == '\r')
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 /* tolerate CRLF in last line */
sz -= 1;
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 hline = PyBytes_FromStringAndSize(l, sz - 1);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 if (!hline) {
return;
}
if (c == ' ' || c == '+') {
PyObject *rline = PyBytes_FromStringAndSize(l + 1, sz - 2);
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyList_SetItem(b, blen - 1, rline);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 }
if (c == ' ' || c == '-') {
Py_INCREF(hline);
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyList_SetItem(a, alen - 1, hline);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 }
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyList_SetItem(hunk, hunksz - 1, hline);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 }
/* python callable form of _fix_newline */
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 static PyObject *fix_newline(PyObject *self, PyObject *args)
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 {
PyObject *hunk, *a, *b;
if (!PyArg_ParseTuple(args, "OOO", &hunk, &a, &b))
return NULL;
_fix_newline(hunk, a, b);
return Py_BuildValue("l", 0);
}
#if (PY_VERSION_HEX < 0x02050000)
static const char *addlines_format = "OOiiOO";
#else
static const char *addlines_format = "OOnnOO";
#endif
/*
* read lines from fp into the hunk. The hunk is parsed into two arrays
* a and b. a gets the old state of the text, b gets the new state
* The control char from the hunk is saved when inserting into a, but not b
* (for performance while deleting files)
*/
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 static PyObject *addlines(PyObject *self, PyObject *args)
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 {
PyObject *fp, *hunk, *a, *b, *x;
Py_ssize_t i;
Py_ssize_t lena, lenb;
Py_ssize_t num;
Py_ssize_t todoa, todob;
char *s, c;
PyObject *l;
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 if (!PyArg_ParseTuple(args, addlines_format, &fp, &hunk, &lena, &lenb,
&a, &b))
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 return NULL;
while (1) {
todoa = lena - PyList_Size(a);
todob = lenb - PyList_Size(b);
num = todoa > todob ? todoa : todob;
if (num == 0)
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 break;
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 for (i = 0; i < num; i++) {
x = PyFile_GetLine(fp, 0);
s = PyBytes_AsString(x);
c = *s;
if (strcmp(s, "\\ No newline at end of file\n") == 0) {
_fix_newline(hunk, a, b);
continue;
}
if (c == '\n') {
/* Some patches may be missing the control char
* on empty lines. Supply a leading space. */
Py_DECREF(x);
x = PyBytes_FromString(" \n");
}
PyList_Append(hunk, x);
if (c == '+') {
l = PyBytes_FromString(s + 1);
PyList_Append(b, l);
Py_DECREF(l);
} else if (c == '-') {
PyList_Append(a, x);
} else {
l = PyBytes_FromString(s + 1);
PyList_Append(b, l);
Py_DECREF(l);
PyList_Append(a, x);
}
Py_DECREF(x);
}
}
return Py_BuildValue("l", 0);
}
/*
* compare the lines in a with the lines in b. a is assumed to have
* a control char at the start of each line, this char is ignored in the
* compare
*/
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 static PyObject *testhunk(PyObject *self, PyObject *args)
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 {
PyObject *a, *b;
long bstart;
Py_ssize_t alen, blen;
Py_ssize_t i;
char *sa, *sb;
if (!PyArg_ParseTuple(args, "OOl", &a, &b, &bstart))
return NULL;
alen = PyList_Size(a);
blen = PyList_Size(b);
if (alen > blen - bstart || bstart < 0) {
return Py_BuildValue("l", -1);
}
for (i = 0; i < alen; i++) {
sa = PyBytes_AsString(PyList_GET_ITEM(a, i));
sb = PyBytes_AsString(PyList_GET_ITEM(b, i + bstart));
if (strcmp(sa + 1, sb) != 0)
return Py_BuildValue("l", -1);
}
return Py_BuildValue("l", 0);
}
static PyMethodDef methods[] = {
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 {"addlines", addlines, METH_VARARGS, "add lines to a hunk\n"},
{"fix_newline", fix_newline, METH_VARARGS, "fixup newline counters\n"},
{"testhunk", testhunk, METH_VARARGS, "test lines in a hunk\n"},
{NULL, NULL}};
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370
static const int version = 1;
#ifdef IS_PY3K
static struct PyModuleDef diffhelpers_module = {
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyModuleDef_HEAD_INIT, "diffhelpers", diffhelpers_doc, -1, methods,
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 };
PyMODINIT_FUNC PyInit_diffhelpers(void)
{
PyObject *m;
m = PyModule_Create(&diffhelpers_module);
if (m == NULL)
return NULL;
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 diffhelpers_Error =
PyErr_NewException("diffhelpers.diffhelpersError", NULL, NULL);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 Py_INCREF(diffhelpers_Error);
PyModule_AddObject(m, "diffhelpersError", diffhelpers_Error);
PyModule_AddIntConstant(m, "version", version);
return m;
}
#else
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 PyMODINIT_FUNC initdiffhelpers(void)
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 {
PyObject *m;
m = Py_InitModule3("diffhelpers", methods, diffhelpers_doc);
Augie Fackler
diffhelpers: allow clang-format oversight...
r36074 diffhelpers_Error =
PyErr_NewException("diffhelpers.diffhelpersError", NULL, NULL);
Yuya Nishihara
diffhelpers: switch to policy importer...
r32370 PyModule_AddIntConstant(m, "version", version);
}
#endif