code
stringlengths 26
870k
| docstring
stringlengths 1
65.6k
| func_name
stringlengths 1
194
| language
stringclasses 1
value | repo
stringlengths 8
68
| path
stringlengths 5
182
| url
stringlengths 46
251
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
def process_set_as_existence_assertion(
ir_set: irast.SetE[irast.Call],
*,
ctx: context.CompilerContextLevel,
) -> SetRVars:
"""Implementation of std::assert_exists"""
expr = ir_set.expr
stmt = ctx.rel
msg_arg = expr.args['message']
ir_arg = expr.args[0]
ir_arg_set = ir_arg.expr
if (
not ir_arg.cardinality.can_be_zero()
and not msg_arg.cardinality.is_multi()
):
# If the argument has been statically proven to be non empty,
# elide the entire assertion.
arg_ref = dispatch.compile(ir_arg_set, ctx=ctx)
pathctx.put_path_value_var(stmt, ir_set.path_id, arg_ref)
pathctx.put_path_id_map(stmt, ir_set.path_id, ir_arg_set.path_id)
return new_stmt_set_rvar(ir_set, stmt, ctx=ctx)
with ctx.subrel() as newctx:
# The solution to assert_exists() is as simple as
# calling raise_on_null().
newctx.expr_exposed = False
newctx.force_optional |= {ir_arg_set.path_id}
pathctx.put_path_id_map(newctx.rel, ir_set.path_id, ir_arg_set.path_id)
arg_ref = dispatch.compile(ir_arg_set, ctx=newctx)
arg_val = output.output_as_value(arg_ref, env=newctx.env)
msg = dispatch.compile(msg_arg.expr, ctx=newctx)
set_expr = pgast.FuncCall(
name=astutils.edgedb_func('raise_on_null', ctx=ctx),
args=[
arg_val,
pgast.StringConstant(val='cardinality_violation'),
pgast.NamedFuncArg(
name='msg',
val=pgast.CoalesceExpr(
args=[
msg,
pgast.StringConstant(
val='assert_exists violation: expression '
'returned an empty set',
),
]
),
),
pgast.NamedFuncArg(
name='constraint',
val=pgast.StringConstant(val='std::assert_exists'),
),
],
)
pathctx.put_path_value_var(
newctx.rel,
ir_arg_set.path_id,
set_expr,
force=True,
)
other_aspect = (
pgce.PathAspect.IDENTITY
if ir_set.path_id.is_objtype_path() else
pgce.PathAspect.SERIALIZED
)
pathctx.put_path_var(
newctx.rel,
ir_arg_set.path_id,
set_expr,
force=True,
aspect=other_aspect,
)
# It is important that we do not provide source, which could allow
# fields on the object to be accessed without triggering the
# raise_on_null. Not providing source means another join is
# needed, which will trigger it.
func_rvar = relctx.new_rel_rvar(ir_set, newctx.rel, ctx=ctx)
relctx.include_rvar(
stmt,
func_rvar,
ir_set.path_id,
aspects=(pgce.PathAspect.VALUE,),
ctx=ctx,
)
return new_stmt_set_rvar(
ir_set,
stmt,
aspects=(pgce.PathAspect.VALUE,),
ctx=ctx,
) | Implementation of std::assert_exists | process_set_as_existence_assertion | python | geldata/gel | edb/pgsql/compiler/relgen.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/relgen.py | Apache-2.0 |
def process_set_as_multiplicity_assertion(
ir_set: irast.SetE[irast.Call],
*,
ctx: context.CompilerContextLevel,
) -> SetRVars:
"""Implementation of std::assert_distinct"""
expr = ir_set.expr
msg_arg = expr.args['message']
ir_arg = expr.args[0]
ir_arg_set = ir_arg.expr
if (
not ir_arg.multiplicity.is_duplicate()
and not msg_arg.cardinality.is_multi()
):
# If the argument has been statically proven to be distinct,
# elide the entire assertion.
arg_ref = dispatch.compile(ir_arg_set, ctx=ctx)
pathctx.put_path_value_var(ctx.rel, ir_set.path_id, arg_ref)
pathctx.put_path_id_map(ctx.rel, ir_set.path_id, ir_arg_set.path_id)
return new_stmt_set_rvar(ir_set, ctx.rel, ctx=ctx)
# Generate a distinct set assertion as the following SQL:
#
# SELECT
# <target_set>,
# (CASE WHEN
# <target_set>
# IS DISTINCT FROM
# lag(<target_set>) OVER (ORDER BY <target_set>)
# THEN <target_set>
# ELSE edgedb.raise(ConstraintViolationError)) AS check_expr
# FROM
# (SELECT <target_set>, row_number() OVER () AS i) AS q
# ORDER BY
# q.i, check_expr
#
# NOTE: sorting over original row_number() is necessary to preserve
# order, as assert_distinct() must be completely transparent for
# compliant sets.
with ctx.subrel() as newctx:
with newctx.subrel() as subctx:
dispatch.visit(ir_arg_set, ctx=subctx)
arg_ref = pathctx.get_path_output(
subctx.rel,
ir_arg_set.path_id,
aspect=pgce.PathAspect.VALUE,
env=subctx.env,
)
arg_val = output.output_as_value(arg_ref, env=newctx.env)
sub_rvar = relctx.new_rel_rvar(ir_arg_set, subctx.rel, ctx=subctx)
aspects = pathctx.list_path_aspects(subctx.rel, ir_arg_set.path_id)
relctx.include_rvar(
newctx.rel, sub_rvar, ir_arg_set.path_id,
aspects=aspects, ctx=subctx,
)
alias = ctx.env.aliases.get('i')
subctx.rel.target_list.append(
pgast.ResTarget(
name=alias,
val=pgast.FuncCall(
name=('row_number',),
args=[],
over=pgast.WindowDef(),
)
)
)
msg = dispatch.compile(msg_arg.expr, ctx=newctx)
do_raise = pgast.FuncCall(
name=astutils.edgedb_func('raise', ctx=ctx),
args=[
pgast.TypeCast(
arg=pgast.NullConstant(),
type_name=pgast.TypeName(
name=pg_types.pg_type_from_ir_typeref(
ir_arg_set.typeref),
),
),
pgast.StringConstant(val='cardinality_violation'),
pgast.NamedFuncArg(
name='msg',
val=pgast.CoalesceExpr(
args=[
msg,
pgast.StringConstant(
val='assert_distinct violation: expression '
'returned a set with duplicate elements',
),
],
),
),
pgast.NamedFuncArg(
name='constraint',
val=pgast.StringConstant(val='std::assert_distinct'),
),
],
)
check_expr = pgast.CaseExpr(
args=[
pgast.CaseWhen(
expr=astutils.new_binop(
lexpr=arg_val,
op='IS DISTINCT FROM',
rexpr=pgast.FuncCall(
name=('lag',),
args=[arg_val],
over=pgast.WindowDef(
order_clause=[pgast.SortBy(node=arg_val)],
),
),
),
result=arg_val,
),
],
defresult=do_raise,
)
alias2 = ctx.env.aliases.get('v')
newctx.rel.target_list.append(
pgast.ResTarget(
val=check_expr,
name=alias2,
)
)
pathctx.put_path_var(
newctx.rel,
ir_set.path_id,
check_expr,
aspect=pgce.PathAspect.VALUE,
)
if newctx.rel.sort_clause is None:
newctx.rel.sort_clause = []
newctx.rel.sort_clause.extend([
pgast.SortBy(
node=pgast.ColumnRef(name=[sub_rvar.alias.aliasname, alias]),
),
pgast.SortBy(
node=pgast.ColumnRef(name=[alias2]),
),
])
pathctx.put_path_id_map(newctx.rel, ir_set.path_id, ir_arg_set.path_id)
func_rvar = relctx.new_rel_rvar(ir_set, newctx.rel, ctx=ctx)
relctx.include_rvar(
ctx.rel, func_rvar, ir_set.path_id, aspects=aspects, ctx=ctx
)
return new_stmt_set_rvar(ir_set, ctx.rel, aspects=aspects, ctx=ctx) | Implementation of std::assert_distinct | process_set_as_multiplicity_assertion | python | geldata/gel | edb/pgsql/compiler/relgen.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/relgen.py | Apache-2.0 |
def _compile_call_args(
ir_set: irast.Set,
*,
skip: Collection[int] = (),
no_subquery_args: bool = False,
ctx: context.CompilerContextLevel,
) -> list[pgast.BaseExpr]:
"""
Compiles function call arguments, whose index is not in `skip`.
"""
expr = ir_set.expr
assert isinstance(expr, irast.Call)
args = []
if isinstance(expr, irast.FunctionCall) and expr.global_args:
for glob_arg in expr.global_args:
arg_ref = dispatch.compile(glob_arg, ctx=ctx)
args.append(output.output_as_value(arg_ref, env=ctx.env))
for ir_key, ir_arg in expr.args.items():
if ir_key in skip:
continue
assert ir_arg.multiplicity != qltypes.Multiplicity.UNKNOWN
typemod = ir_arg.param_typemod
# Support a mode where we try to compile arguments as pure
# subqueries. This is occasionally valuable as it lets us
# "push down" the subqueries from the top level, which is
# important for things like hitting pgvector indexes in an
# ORDER BY.
arg_typeref = ir_arg.expr.typeref
make_subquery = (
expr.prefer_subquery_args
and typemod != qltypes.TypeModifier.SetOfType
and ir_arg.cardinality.is_single()
and (arg_typeref.is_scalar or arg_typeref.collection)
and not _needs_arg_null_check(expr, ir_arg, typemod, ctx=ctx)
and not no_subquery_args
)
if make_subquery:
arg_ref = set_as_subquery(ir_arg.expr, as_value=True, ctx=ctx)
arg_ref.nullable = ir_arg.cardinality.can_be_zero()
arg_ref = astutils.collapse_query(arg_ref)
else:
arg_ref = dispatch.compile(ir_arg.expr, ctx=ctx)
arg_ref = output.output_as_value(arg_ref, env=ctx.env)
args.append(arg_ref)
_compile_arg_null_check(expr, ir_arg, arg_ref, typemod, ctx=ctx)
if (
isinstance(expr, irast.FunctionCall)
and ir_arg.expr_type_path_id is not None
):
# Object type arguments are represented by two
# SQL arguments: object id and object type id.
# The latter is needed for proper overload
# dispatch.
ensure_source_rvar(ir_arg.expr, ctx.rel, ctx=ctx)
type_ref = relctx.get_path_var(
ctx.rel,
ir_arg.expr_type_path_id,
aspect=pgce.PathAspect.IDENTITY,
ctx=ctx,
)
args.append(type_ref)
if (
isinstance(expr, irast.FunctionCall)
and expr.has_empty_variadic
and expr.variadic_param_type is not None
):
var = pgast.TypeCast(
arg=pgast.ArrayExpr(elements=[]),
type_name=pgast.TypeName(
name=pg_types.pg_type_from_ir_typeref(
expr.variadic_param_type)
)
)
args.append(pgast.VariadicArgument(expr=var))
return args | Compiles function call arguments, whose index is not in `skip`. | _compile_call_args | python | geldata/gel | edb/pgsql/compiler/relgen.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/relgen.py | Apache-2.0 |
def create_subrel_for_expr(
expr: pgast.BaseExpr, *, ctx: context.CompilerContextLevel
) -> irast.PathId:
"""
Creates a sub query relation that contains the given expression.
"""
# create a dummy path id for a dummy object
expr_id = irast.PathId.new_dummy(ctx.env.aliases.get('d'))
with ctx.subrel() as newctx:
# register the expression
pathctx.put_path_var(
newctx.rel,
expr_id,
expr,
aspect=pgce.PathAspect.VALUE,
)
# include the subrel in the parent
new_rvar = relctx.rvar_for_rel(newctx.rel, ctx=ctx)
relctx.include_rvar(
ctx.rel,
new_rvar,
expr_id,
aspects=(pgce.PathAspect.VALUE,),
ctx=ctx,
)
return expr_id | Creates a sub query relation that contains the given expression. | create_subrel_for_expr | python | geldata/gel | edb/pgsql/compiler/relgen.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/relgen.py | Apache-2.0 |
def init_dml_stmt(
ir_stmt: irast.MutatingStmt,
*,
ctx: context.CompilerContextLevel,
) -> DMLParts:
"""Prepare the common structure of the query representing a DML stmt.
Args:
ir_stmt:
IR of the DML statement.
Returns:
A ``DMLParts`` tuple containing a map of DML CTEs as well as the
common range CTE for UPDATE/DELETE statements.
"""
range_cte: Optional[pgast.CommonTableExpr]
range_rvar: Optional[pgast.RelRangeVar]
clauses.compile_volatile_bindings(ir_stmt, ctx=ctx)
if isinstance(ir_stmt, (irast.UpdateStmt, irast.DeleteStmt)):
# UPDATE and DELETE operate over a range, so generate
# the corresponding CTE and connect it to the DML statements.
range_cte = get_dml_range(ir_stmt, ctx=ctx)
range_rvar = pgast.RelRangeVar(
relation=range_cte,
alias=pgast.Alias(
aliasname=ctx.env.aliases.get(hint='range')
)
)
else:
range_cte = None
range_rvar = None
top_typeref = ir_stmt.material_type
typerefs = [top_typeref]
if isinstance(ir_stmt, (irast.UpdateStmt, irast.DeleteStmt)):
if top_typeref.union:
for component in top_typeref.union:
if component.material_type:
component = component.material_type
typerefs.append(component)
typerefs.extend(irtyputils.get_typeref_descendants(component))
typerefs.extend(irtyputils.get_typeref_descendants(top_typeref))
# Only update/delete concrete types. (Except in the degenerate
# corner case where there are none, in which case keep using
# everything so as to avoid needing a more complex special case.)
concrete_typerefs = [t for t in typerefs if not t.is_abstract]
if concrete_typerefs:
typerefs = concrete_typerefs
dml_map = {}
for typeref in typerefs:
if typeref.union:
continue
if (
isinstance(typeref.name_hint, sn.QualName)
and typeref.name_hint.module in ('sys', 'cfg')
):
continue
dml_cte, dml_rvar = gen_dml_cte(
ir_stmt,
range_rvar=range_rvar,
typeref=typeref,
ctx=ctx,
)
dml_map[typeref] = (dml_cte, dml_rvar)
else_cte = None
if (
isinstance(ir_stmt, irast.InsertStmt)
and ir_stmt.on_conflict and ir_stmt.on_conflict.else_ir is not None
):
dml_cte = pgast.CommonTableExpr(
query=pgast.SelectStmt(),
name=ctx.env.aliases.get(hint='melse'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
dml_rvar = relctx.rvar_for_rel(dml_cte, ctx=ctx)
else_cte = (dml_cte, dml_rvar)
put_iterator_bond(ctx.enclosing_cte_iterator, ctx.rel)
ctx.dml_stmt_stack.append(ir_stmt)
return DMLParts(
dml_ctes=dml_map,
range_cte=range_cte,
else_cte=else_cte,
) | Prepare the common structure of the query representing a DML stmt.
Args:
ir_stmt:
IR of the DML statement.
Returns:
A ``DMLParts`` tuple containing a map of DML CTEs as well as the
common range CTE for UPDATE/DELETE statements. | init_dml_stmt | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def get_dml_range(
ir_stmt: Union[irast.UpdateStmt, irast.DeleteStmt],
*,
ctx: context.CompilerContextLevel,
) -> pgast.CommonTableExpr:
"""Create a range CTE for the given DML statement.
Args:
ir_stmt:
IR of the DML statement.
Returns:
A CommonTableExpr node representing the range affected
by the DML statement.
"""
target_ir_set = ir_stmt.subject
ir_qual_expr = ir_stmt.where
ir_qual_card = ir_stmt.where_card
with ctx.newrel() as subctx:
subctx.expr_exposed = False
range_stmt = subctx.rel
merge_iterator(ctx.enclosing_cte_iterator, range_stmt, ctx=subctx)
dispatch.visit(target_ir_set, ctx=subctx)
pathctx.get_path_identity_output(
range_stmt, target_ir_set.path_id, env=subctx.env)
if ir_qual_expr is not None:
with subctx.new() as wctx:
clauses.setup_iterator_volatility(target_ir_set, ctx=wctx)
range_stmt.where_clause = astutils.extend_binop(
range_stmt.where_clause,
clauses.compile_filter_clause(
ir_qual_expr, ir_qual_card, ctx=wctx))
range_stmt.path_id_mask.discard(target_ir_set.path_id)
pathctx.put_path_bond(range_stmt, target_ir_set.path_id)
range_cte = pgast.CommonTableExpr(
query=range_stmt,
name=ctx.env.aliases.get('range'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
return range_cte | Create a range CTE for the given DML statement.
Args:
ir_stmt:
IR of the DML statement.
Returns:
A CommonTableExpr node representing the range affected
by the DML statement. | get_dml_range | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def _mk_dynamic_get_path(
ptr_map: Dict[sn.Name, pgast.BaseExpr],
typeref: irast.TypeRef,
fallback_rvar: Optional[pgast.PathRangeVar] = None,
) -> pgast.DynamicRangeVarFunc:
"""A dynamic rvar function for insert/update.
It returns values out of a select purely based on material rptr,
as if it was a base relation. This is to make it easy for access
policies to operate on the results.
"""
def dynamic_get_path(
rel: pgast.Query, path_id: irast.PathId, *,
flavor: str,
aspect: str, env: context.Environment
) -> Optional[pgast.BaseExpr | pgast.PathRangeVar]:
if (
flavor != 'normal'
or aspect not in (
pgce.PathAspect.VALUE, pgce.PathAspect.IDENTITY
)
):
return None
if rptr := path_id.rptr():
if ret := ptr_map.get(rptr.real_material_ptr.name):
return ret
if rptr.real_material_ptr.shortname.name == '__type__':
return astutils.compile_typeref(typeref)
# If a fallback rvar is specified, defer to that.
# This is used in rewrites to go back to the original
if fallback_rvar:
return fallback_rvar
if not rptr:
raise LookupError('only pointers appear in insert fallback')
# Properties that aren't specified are {}
return pgast.NullConstant()
return dynamic_get_path | A dynamic rvar function for insert/update.
It returns values out of a select purely based on material rptr,
as if it was a base relation. This is to make it easy for access
policies to operate on the results. | _mk_dynamic_get_path | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def process_insert_body(
*,
ir_stmt: irast.InsertStmt,
insert_cte: pgast.CommonTableExpr,
dml_parts: DMLParts,
ctx: context.CompilerContextLevel,
) -> None:
"""Generate SQL DML CTEs from an InsertStmt IR.
Args:
ir_stmt:
IR of the DML statement.
insert_cte:
A CommonTableExpr node representing the SQL INSERT into
the main relation of the DML subject.
else_cte_rvar:
If present, a tuple containing a CommonTableExpr and
a RangeVar for it, which represent the body of an
ELSE clause in an UNLESS CONFLICT construct.
dml_parts:
A DMLParts tuple returned by init_dml_stmt().
"""
# We build the tuples to insert in a select we put into a CTE
select = pgast.SelectStmt(target_list=[])
# The main INSERT query of this statement will always be
# present to insert at least the `id` property.
insert_stmt = insert_cte.query
assert isinstance(insert_stmt, pgast.InsertStmt)
typeref = ir_stmt.subject.typeref.real_material_type
# Handle an UNLESS CONFLICT if we need it
# If there is an UNLESS CONFLICT, we need to know that there is a
# conflict *before* we execute DML for fields stored in the object
# itself, so we can prevent that execution from happening. If that
# is necessary, compile_insert_else_body will generate an iterator
# CTE with a row for each non-conflicting insert we want to do. We
# then use that as the iterator for any DML in inline fields.
#
# (For DML in the definition of pointers stored in link tables, we
# don't need to worry about this, because we can run that DML
# after the enclosing INSERT, using the enclosing INSERT as the
# iterator.)
on_conflict_fake_iterator = None
if ir_stmt.on_conflict:
assert not insert_stmt.on_conflict
on_conflict_fake_iterator = compile_insert_else_body(
insert_stmt,
ir_stmt,
ir_stmt.on_conflict,
ctx.enclosing_cte_iterator,
dml_parts.else_cte,
ctx=ctx,
)
iterator = ctx.enclosing_cte_iterator
inner_iterator = on_conflict_fake_iterator or iterator
# ptr_map needs to be set up in advance of compiling the shape
# because defaults might reference earlier pointers.
ptr_map: Dict[sn.Name, pgast.BaseExpr] = {}
# Use a dynamic rvar to return values out of the select purely
# based on material rptr, as if it was a base relation.
# This is to make it easy for access policies to operate on the result
# of the INSERT.
fallback_rvar = pgast.DynamicRangeVar(
dynamic_get_path=_mk_dynamic_get_path(ptr_map, typeref))
pathctx.put_path_source_rvar(
select, ir_stmt.subject.path_id, fallback_rvar
)
pathctx.put_path_value_rvar(select, ir_stmt.subject.path_id, fallback_rvar)
# compile contents CTE
elements: List[Tuple[irast.SetE[irast.Pointer], irast.BasePointerRef]] = []
for shape_el, shape_op in ir_stmt.subject.shape:
assert shape_op is qlast.ShapeOp.ASSIGN
# If the shape element is a linkprop, we do nothing.
# It will be picked up by the enclosing DML.
if shape_el.path_id.is_linkprop_path():
continue
ptrref = shape_el.expr.ptrref
if ptrref.material_ptr is not None:
ptrref = ptrref.material_ptr
assert shape_el.expr.expr
elements.append((shape_el, ptrref))
external_inserts = process_insert_shape(
ir_stmt, select, ptr_map, elements, iterator, inner_iterator, ctx
)
single_external = [
ir for ir in external_inserts
if ir.expr.dir_cardinality.is_single()
]
# Put the select that builds the tuples to insert into its own CTE.
# We do this for two reasons:
# 1. Generating the object ids outside of the actual SQL insert allows
# us to join any enclosing iterators into any nested external inserts.
# 2. We can use the contents CTE to evaluate insert access policies
# before we actually try the insert. This is important because
# otherwise an exclusive constraint could be raised first,
# which leaks information.
pathctx.put_path_bond(select, ir_stmt.subject.path_id)
contents_cte = pgast.CommonTableExpr(
query=select,
name=ctx.env.aliases.get('ins_contents'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
ctx.toplevel_stmt.append_cte(contents_cte)
contents_rvar = relctx.rvar_for_rel(contents_cte, ctx=ctx)
rewrites = ir_stmt.rewrites and ir_stmt.rewrites.by_type.get(typeref)
pol_expr = ir_stmt.write_policies.get(typeref.id)
pol_ctx = None
if pol_expr or rewrites or single_external:
# Create a context for handling policies/rewrites that we will
# use later. We do this in advance so that the link update code
# can populate overlay fields in it.
with ctx.new() as pol_ctx:
pass
needs_insert_on_conflict = bool(
ir_stmt.on_conflict and not on_conflict_fake_iterator)
# The first serious bit of trickiness: if there are rewrites, the link
# table updates need to be done *before* we compute the rewrites, since
# the rewrites might refer to them.
#
# However, we can't unconditionally do it like this, because we
# want to be able to use ON CONFLICT to implement UNLESS CONFLICT
# ON when possible, and in that case the link table operations
# need to be done after the *actual insert*, because it is the actual
# insert that filters out conflicting rows. (This also means that we
# can't use ON CONFLICT if there are rewrites.)
#
# Similar issues obtain with access policies: we can't use ON
# CONFLICT if there are access policies, since we can't "see" all
# possible conflicting objects.
#
# We *also* need link tables to go first if there are any single links
# with link properties. We do the actual computation for those in a link
# table and then join it in to the main table, where it is duplicated.
link_ctes = []
def _update_link_tables(inp_cte: pgast.CommonTableExpr) -> None:
# Process necessary updates to the link tables.
for shape_el in external_inserts:
link_cte, check_cte = process_link_update(
ir_stmt=ir_stmt,
ir_set=shape_el,
dml_cte=inp_cte,
source_typeref=typeref,
iterator=iterator,
policy_ctx=pol_ctx,
ctx=ctx,
)
if link_cte:
link_ctes.append(link_cte)
if check_cte is not None:
ctx.env.check_ctes.append(check_cte)
if not needs_insert_on_conflict:
_update_link_tables(contents_cte)
# compile rewrites CTE
if rewrites or single_external:
rewrites = rewrites or {}
assert not needs_insert_on_conflict
assert pol_ctx
with pol_ctx.reenter(), pol_ctx.newrel() as rctx:
# Pull in ptr rel overlays, so we can see the pointers
merge_overlays_globally((ir_stmt,), ctx=rctx)
contents_cte, contents_rvar = process_insert_rewrites(
ir_stmt,
contents_cte=contents_cte,
iterator=iterator,
inner_iterator=inner_iterator,
rewrites=rewrites,
single_external=single_external,
elements=elements,
ctx=rctx,
)
# Populate the real insert statement based on the select we generated
insert_stmt.cols = [
pgast.InsertTarget(name=name)
for value in contents_cte.query.target_list
# Filter out generated columns; only keep concrete ones
if '~' not in (name := not_none(value.name))
]
insert_stmt.select_stmt = pgast.SelectStmt(
target_list=[
pgast.ResTarget(val=col) for col in insert_stmt.cols
],
from_clause=[contents_rvar],
)
pathctx.put_path_bond(insert_stmt, ir_stmt.subject.path_id)
real_insert_cte = pgast.CommonTableExpr(
query=insert_stmt,
name=ctx.env.aliases.get('ins'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
# Create the final CTE for the insert that joins the insert
# and the select together.
with ctx.newrel() as ictx:
merge_iterator(iterator, ictx.rel, ctx=ictx)
insert_rvar = relctx.rvar_for_rel(real_insert_cte, ctx=ctx)
relctx.include_rvar(
ictx.rel, insert_rvar, ir_stmt.subject.path_id, ctx=ictx)
relctx.include_rvar(
ictx.rel, contents_rvar, ir_stmt.subject.path_id, ctx=ictx)
# TODO: set up dml_parts with a SelectStmt for inserts always?
insert_cte.query = ictx.rel
# If there is an ON CONFLICT clause, insert the CTEs now so that the
# link inserts can depend on it. Otherwise we have the link updates
# depend on the contents cte so that policies can operate before
# doing any actual INSERTs.
if needs_insert_on_conflict:
ctx.toplevel_stmt.append_cte(real_insert_cte)
ctx.toplevel_stmt.append_cte(insert_cte)
link_op_cte = insert_cte
else:
link_op_cte = contents_cte
if needs_insert_on_conflict:
_update_link_tables(link_op_cte)
if pol_expr:
assert pol_ctx
assert not needs_insert_on_conflict
with pol_ctx.reenter():
policy_cte = compile_policy_check(
contents_cte, ir_stmt, pol_expr, typeref=typeref, ctx=pol_ctx
)
force_policy_checks(
policy_cte,
(insert_stmt,) + tuple(cte.query for cte in link_ctes),
ctx=ctx)
for link_cte in link_ctes:
ctx.toplevel_stmt.append_cte(link_cte)
if not needs_insert_on_conflict:
ctx.toplevel_stmt.append_cte(real_insert_cte)
ctx.toplevel_stmt.append_cte(insert_cte)
for extra_conflict in (ir_stmt.conflict_checks or ()):
compile_insert_else_body(
insert_stmt,
ir_stmt,
extra_conflict,
inner_iterator,
None,
ctx=ctx,
) | Generate SQL DML CTEs from an InsertStmt IR.
Args:
ir_stmt:
IR of the DML statement.
insert_cte:
A CommonTableExpr node representing the SQL INSERT into
the main relation of the DML subject.
else_cte_rvar:
If present, a tuple containing a CommonTableExpr and
a RangeVar for it, which represent the body of an
ELSE clause in an UNLESS CONFLICT construct.
dml_parts:
A DMLParts tuple returned by init_dml_stmt(). | process_insert_body | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def process_update_body(
*,
ir_stmt: irast.UpdateStmt,
update_cte: pgast.CommonTableExpr,
dml_parts: DMLParts,
typeref: irast.TypeRef,
ctx: context.CompilerContextLevel,
) -> None:
"""Generate SQL DML CTEs from an UpdateStmt IR.
Args:
ir_stmt:
IR of the DML statement.
update_cte:
CTE representing the SQL UPDATE to the main relation of the UPDATE
subject.
dml_parts:
A DMLParts tuple returned by init_dml_stmt().
typeref:
A TypeRef corresponding the the type of a subject being updated
by the update_cte.
"""
assert isinstance(update_cte.query, pgast.SelectStmt)
contents_select = update_cte.query
toplevel = ctx.toplevel_stmt
put_iterator_bond(ctx.enclosing_cte_iterator, contents_select)
assert dml_parts.range_cte
iterator = pgast.IteratorCTE(
path_id=ir_stmt.subject.path_id,
cte=dml_parts.range_cte,
parent=ctx.enclosing_cte_iterator,
)
with ctx.newscope() as subctx:
# It is necessary to process the expressions in
# the UpdateStmt shape body in the context of the
# UPDATE statement so that references to the current
# values of the updated object are resolved correctly.
subctx.parent_rel = contents_select
subctx.expr_exposed = False
subctx.enclosing_cte_iterator = iterator
clauses.setup_iterator_volatility(iterator, ctx=subctx)
# compile contents CTE
elements = [
(shape_el, shape_el.expr.ptrref, shape_op)
for shape_el, shape_op in ir_stmt.subject.shape
if shape_op != qlast.ShapeOp.MATERIALIZE
]
values, external_updates, ptr_map = process_update_shape(
ir_stmt, contents_select, elements, typeref, subctx
)
relation = contents_select.from_clause[0]
assert isinstance(relation, pgast.PathRangeVar)
# Use a dynamic rvar to return values out of the select purely
# based on material rptr, as if it was a base relation (and to
# fall back to the base relation if the value wasn't updated.)
fallback_rvar = pgast.DynamicRangeVar(
dynamic_get_path=_mk_dynamic_get_path(ptr_map, typeref, relation),
)
pathctx.put_path_source_rvar(
contents_select,
ir_stmt.subject.path_id,
fallback_rvar,
)
pathctx.put_path_value_rvar(
contents_select,
ir_stmt.subject.path_id,
fallback_rvar,
)
update_stmt = None
single_external = [
ir for ir, _ in external_updates
if ir.expr.dir_cardinality.is_single()
]
rewrites = ir_stmt.rewrites and ir_stmt.rewrites.by_type.get(typeref)
pol_expr = ir_stmt.write_policies.get(typeref.id)
pol_ctx = None
if pol_expr or rewrites or single_external:
# Create a context for handling policies/rewrites that we will
# use later. We do this in advance so that the link update code
# can populate overlay fields in it.
with ctx.new() as pol_ctx:
pass
no_update = not values and not rewrites and not single_external
if no_update:
# No updates directly to the set target table,
# so convert the UPDATE statement into a SELECT.
update_cte.query = contents_select
contents_cte = update_cte
else:
contents_cte = pgast.CommonTableExpr(
query=contents_select,
name=ctx.env.aliases.get("upd_contents"),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
toplevel.append_cte(contents_cte)
# Process necessary updates to the link tables.
# We do link tables before we do the main update so that
link_ctes = []
for expr, shape_op in external_updates:
link_cte, check_cte = process_link_update(
ir_stmt=ir_stmt,
ir_set=expr,
dml_cte=contents_cte,
iterator=iterator,
shape_op=shape_op,
source_typeref=typeref,
ctx=ctx,
policy_ctx=pol_ctx,
)
if link_cte:
link_ctes.append(link_cte)
if check_cte is not None:
ctx.env.check_ctes.append(check_cte)
if not no_update:
table_relation = contents_select.from_clause[0]
assert isinstance(table_relation, pgast.RelRangeVar)
range_relation = contents_select.from_clause[1]
assert isinstance(range_relation, pgast.PathRangeVar)
contents_rvar = relctx.rvar_for_rel(contents_cte, ctx=ctx)
subject_path_id = ir_stmt.subject.path_id
# Compile rewrites CTE
if rewrites or single_external:
rewrites = rewrites or {}
assert pol_ctx
with pol_ctx.reenter(), pol_ctx.new() as rctx:
merge_overlays_globally((ir_stmt,), ctx=rctx)
contents_cte, contents_rvar, values = process_update_rewrites(
ir_stmt,
typeref=typeref,
contents_cte=contents_cte,
contents_rvar=contents_rvar,
iterator=iterator,
contents_select=contents_select,
table_relation=table_relation,
range_relation=range_relation,
single_external=single_external,
rewrites=rewrites,
elements=elements,
ctx=rctx,
)
update_stmt = pgast.UpdateStmt(
relation=table_relation,
where_clause=astutils.new_binop(
lexpr=pgast.ColumnRef(
name=[table_relation.alias.aliasname, "id"]
),
op="=",
rexpr=pathctx.get_rvar_path_identity_var(
contents_rvar, subject_path_id, env=ctx.env
),
),
from_clause=[contents_rvar],
targets=[
pgast.MultiAssignRef(
columns=[not_none(value.name) for value, _ in values],
source=pgast.SelectStmt(
target_list=[
pgast.ResTarget(
val=pgast.ColumnRef(
name=[
contents_rvar.alias.aliasname,
not_none(value.name),
]
)
)
for value, _ in values
],
),
)
],
)
relctx.pull_path_namespace(
target=update_stmt, source=contents_rvar, ctx=ctx
)
pathctx.put_path_value_rvar(
update_stmt, subject_path_id, table_relation
)
pathctx.put_path_source_rvar(
update_stmt, subject_path_id, table_relation
)
put_iterator_bond(ctx.enclosing_cte_iterator, update_stmt)
update_cte.query = update_stmt
if pol_expr:
assert pol_ctx
with pol_ctx.reenter():
policy_cte = compile_policy_check(
contents_cte, ir_stmt, pol_expr, typeref=typeref, ctx=pol_ctx
)
force_policy_checks(
policy_cte,
((update_stmt,) if update_stmt else ())
+ tuple(cte.query for cte in link_ctes),
ctx=ctx,
)
if values:
toplevel.append_cte(update_cte)
for link_cte in link_ctes:
toplevel.append_cte(link_cte) | Generate SQL DML CTEs from an UpdateStmt IR.
Args:
ir_stmt:
IR of the DML statement.
update_cte:
CTE representing the SQL UPDATE to the main relation of the UPDATE
subject.
dml_parts:
A DMLParts tuple returned by init_dml_stmt().
typeref:
A TypeRef corresponding the the type of a subject being updated
by the update_cte. | process_update_body | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def check_update_type(
val: pgast.BaseExpr,
rel_or_rvar: Union[pgast.BaseExpr, pgast.PathRangeVar],
*,
is_subquery: bool,
ir_stmt: irast.UpdateStmt,
ir_set: irast.Set,
shape_ptrref: irast.BasePointerRef,
actual_ptrref: irast.BasePointerRef,
ctx: context.CompilerContextLevel,
) -> pgast.BaseExpr:
"""Possibly insert a type check on an UPDATE to a link
Because edgedb allows subtypes to covariantly override the target
types of links, we need to insert runtime type checks when
the target in a base type being UPDATEd does not match the
target type for this concrete subtype being handled.
"""
assert isinstance(actual_ptrref, irast.PointerRef)
base_ptrref = shape_ptrref.real_material_ptr
# We skip the check if either the base type matches exactly
# or the shape type matches exactly. FIXME: *Really* we want to do
# a subtype check, here, though, since this could do a needless
# check if we have multiple levels of overloading, but we don't
# have the infrastructure here.
if (
not irtyputils.is_object(ir_set.typeref)
or base_ptrref.out_target.id == actual_ptrref.out_target.id
or shape_ptrref.out_target.id == actual_ptrref.out_target.id
):
return val
if isinstance(rel_or_rvar, pgast.PathRangeVar):
rvar = rel_or_rvar
else:
assert isinstance(rel_or_rvar, pgast.BaseRelation)
rvar = relctx.rvar_for_rel(rel_or_rvar, ctx=ctx)
# Make up a ptrref for the __type__ link on our actual target type
# and make up a new path_id to access it. Relies on __type__ always
# being named __type__, but that's fine.
# (Arranging to actually get a legit pointer ref is pointlessly expensive.)
el_name = sn.QualName('__', '__type__')
actual_type_ptrref = irast.SpecialPointerRef(
name=el_name,
shortname=el_name,
out_source=actual_ptrref.out_target,
# HACK: This is obviously not the right target type, but we don't
# need it for anything and the pathid never escapes this function.
out_target=actual_ptrref.out_target,
out_cardinality=qltypes.Cardinality.AT_MOST_ONE,
)
type_pathid = ir_set.path_id.extend(ptrref=actual_type_ptrref)
# Grab the actual value we have inserted and pull the __type__ out
rval = pathctx.get_rvar_path_identity_var(
rvar, ir_set.path_id, env=ctx.env)
typ = pathctx.get_rvar_path_identity_var(rvar, type_pathid, env=ctx.env)
typeref_val = dispatch.compile(actual_ptrref.out_target, ctx=ctx)
# Do the check! Include the ptrref for this concrete class and
# also the (dynamic) type of the argument, so that we can produce
# a good error message.
check_result = pgast.FuncCall(
name=astutils.edgedb_func('issubclass', ctx=ctx),
args=[typ, typeref_val],
)
maybe_null = pgast.CaseExpr(
args=[pgast.CaseWhen(expr=check_result, result=rval)])
maybe_raise = pgast.FuncCall(
name=astutils.edgedb_func('raise_on_null', ctx=ctx),
args=[
maybe_null,
pgast.StringConstant(val='wrong_object_type'),
pgast.NamedFuncArg(
name='msg',
val=pgast.StringConstant(val='covariance error')
),
pgast.NamedFuncArg(
name='column',
val=pgast.StringConstant(val=str(actual_ptrref.id)),
),
pgast.NamedFuncArg(
name='table',
val=pgast.TypeCast(
arg=typ, type_name=pgast.TypeName(name=('text',))
),
),
],
)
if is_subquery:
# If this is supposed to be a subquery (because it is an
# update of a single link), wrap the result query in a new one,
# since we need to access two outputs from it and produce just one
# from this query
return pgast.SelectStmt(
from_clause=[rvar],
target_list=[pgast.ResTarget(val=maybe_raise)],
)
else:
return maybe_raise | Possibly insert a type check on an UPDATE to a link
Because edgedb allows subtypes to covariantly override the target
types of links, we need to insert runtime type checks when
the target in a base type being UPDATEd does not match the
target type for this concrete subtype being handled. | check_update_type | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def process_link_update(
*,
ir_stmt: irast.MutatingStmt,
ir_set: irast.SetE[irast.Pointer],
shape_op: qlast.ShapeOp = qlast.ShapeOp.ASSIGN,
source_typeref: irast.TypeRef,
dml_cte: pgast.CommonTableExpr,
iterator: Optional[pgast.IteratorCTE] = None,
ctx: context.CompilerContextLevel,
policy_ctx: Optional[context.CompilerContextLevel],
) -> Tuple[Optional[pgast.CommonTableExpr], Optional[pgast.CommonTableExpr]]:
"""Perform updates to a link relation as part of a DML statement.
Args:
ir_stmt:
IR of the DML statement.
ir_set:
IR of the INSERT/UPDATE body element.
shape_op:
The operation of the UPDATE body element (:=, +=, -=). For
INSERT this should always be :=.
source_typeref:
An ir.TypeRef instance representing the specific type of an object
being updated.
dml_cte:
CTE representing the SQL INSERT or UPDATE to the main
relation of the DML subject.
iterator:
IR and CTE representing the iterator range in the FOR clause
of the EdgeQL DML statement (if present).
policy_ctx:
Optionally, a context in which to populate overlays that
use the select CTE for overlays instead of the
actual insert CTE. This is needed if an access policy is to
be applied, and requires disabling a potential optimization.
We need separate overlay contexts because default values for
link properties don't currently get populated in our IR, so we
need to do actual SQL DML to get their values. (And so we disallow
their use in policies.)
"""
toplevel = ctx.toplevel_stmt
is_insert = isinstance(ir_stmt, irast.InsertStmt)
rptr = ir_set.expr
ptrref = rptr.ptrref
assert isinstance(ptrref, irast.PointerRef)
target_is_scalar = not irtyputils.is_object(ir_set.typeref)
path_id = ir_set.path_id
# The links in the dml class shape have been derived,
# but we must use the correct specialized link class for the
# base material type.
mptrref = irtyputils.find_actual_ptrref(source_typeref, ptrref)
assert isinstance(mptrref, irast.PointerRef)
target_rvar = relctx.range_for_ptrref(
mptrref, for_mutation=True, only_self=True, ctx=ctx)
assert isinstance(target_rvar, pgast.RelRangeVar)
assert isinstance(target_rvar.relation, pgast.Relation)
target_alias = target_rvar.alias.aliasname
dml_cte_rvar = pgast.RelRangeVar(
relation=dml_cte,
alias=pgast.Alias(
aliasname=ctx.env.aliases.get('m')
)
)
# Turn the IR of the expression on the right side of :=
# into a subquery returning records for the link table.
data_cte, specified_cols = process_link_values(
ir_stmt=ir_stmt,
ir_expr=ir_set,
dml_rvar=dml_cte_rvar,
source_typeref=source_typeref,
target_is_scalar=target_is_scalar,
enforce_cardinality=(shape_op is qlast.ShapeOp.ASSIGN),
dml_cte=dml_cte,
iterator=iterator,
ctx=ctx,
)
toplevel.append_cte(data_cte)
delqry: Optional[pgast.DeleteStmt]
data_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
val=pgast.ColumnRef(
name=[data_cte.name, pgast.Star()]
),
),
],
from_clause=[
pgast.RelRangeVar(relation=data_cte),
],
)
if not is_insert and shape_op is not qlast.ShapeOp.APPEND:
source_ref = pathctx.get_rvar_path_identity_var(
dml_cte_rvar,
ir_stmt.subject.path_id,
env=ctx.env,
)
if shape_op is qlast.ShapeOp.SUBTRACT:
data_rvar = relctx.rvar_for_rel(data_select, ctx=ctx)
if target_is_scalar:
# MULTI properties are not distinct, and since `-=` must
# be a proper inverse of `+=` we cannot simply DELETE
# all property values matching the `-=` expression, and
# instead have to resort to careful deletion of no more
# than the number of tuples returned by the expression.
# Here, we rely on the "ctid" system column to refer to
# specific tuples.
#
# DELETE
# FROM <link-tab>
# WHERE
# ctid IN (
# SELECT
# shortlist.ctid
# FROM
# (SELECT
# source,
# target,
# count(target) AS cnt
# FROM
# <data-expr>
# GROUP BY source, target
# ) AS counts,
# LATERAL (
# SELECT
# candidates.ctid
# FROM
# (SELECT
# ctid,
# row_number() OVER (
# PARTITION BY data
# ORDER BY data
# ) AS rn
# FROM
# <link-tab>
# WHERE
# source = counts.source
# AND target = counts.target
# ) AS candidates
# WHERE
# candidates.rn <= counts.cnt
# ) AS shortlist
# );
val_src_ref = pgast.ColumnRef(
name=[data_rvar.alias.aliasname, 'source'],
)
val_tgt_ref = pgast.ColumnRef(
name=[data_rvar.alias.aliasname, 'target'],
)
counts_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(name='source', val=val_src_ref),
pgast.ResTarget(name='target', val=val_tgt_ref),
pgast.ResTarget(
name='cnt',
val=pgast.FuncCall(
name=('count',),
args=[val_tgt_ref],
),
),
],
from_clause=[data_rvar],
group_clause=[val_src_ref, val_tgt_ref],
)
counts_rvar = relctx.rvar_for_rel(counts_select, ctx=ctx)
counts_alias = counts_rvar.alias.aliasname
target_ref = pgast.ColumnRef(name=[target_alias, 'target'])
candidates_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
name='ctid',
val=pgast.ColumnRef(
name=[target_alias, 'ctid'],
),
),
pgast.ResTarget(
name='rn',
val=pgast.FuncCall(
name=('row_number',),
args=[],
over=pgast.WindowDef(
partition_clause=[target_ref],
order_clause=[
pgast.SortBy(node=target_ref),
],
),
),
),
],
from_clause=[target_rvar],
where_clause=astutils.new_binop(
lexpr=astutils.new_binop(
lexpr=pgast.ColumnRef(
name=[counts_alias, 'source'],
),
op='=',
rexpr=pgast.ColumnRef(
name=[target_alias, 'source'],
),
),
op='AND',
rexpr=astutils.new_binop(
lexpr=target_ref,
op='=',
rexpr=pgast.ColumnRef(
name=[counts_alias, 'target']),
),
),
)
candidates_rvar = relctx.rvar_for_rel(
candidates_select, ctx=ctx)
candidates_alias = candidates_rvar.alias.aliasname
shortlist_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
name='ctid',
val=pgast.ColumnRef(
name=[candidates_alias, 'ctid'],
),
),
],
from_clause=[candidates_rvar],
where_clause=astutils.new_binop(
lexpr=pgast.ColumnRef(name=[candidates_alias, 'rn']),
op='<=',
rexpr=pgast.ColumnRef(name=[counts_alias, 'cnt']),
),
)
shortlist_rvar = relctx.rvar_for_rel(
shortlist_select, lateral=True, ctx=ctx)
shortlist_alias = shortlist_rvar.alias.aliasname
ctid_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
name='ctid',
val=pgast.ColumnRef(name=[shortlist_alias, 'ctid'])
),
],
from_clause=[
counts_rvar,
shortlist_rvar,
],
)
delqry = pgast.DeleteStmt(
relation=target_rvar,
where_clause=astutils.new_binop(
lexpr=pgast.ColumnRef(
name=[target_alias, 'ctid'],
),
op='=',
rexpr=pgast.SubLink(
operator="ANY",
expr=ctid_select,
),
),
returning_list=[
pgast.ResTarget(
val=pgast.ColumnRef(
name=[target_alias, pgast.Star()],
),
)
]
)
else:
# Links are always distinct, so we can simply
# DELETE the tuples matching the `-=` expression.
delqry = pgast.DeleteStmt(
relation=target_rvar,
where_clause=astutils.new_binop(
lexpr=astutils.new_binop(
lexpr=source_ref,
op='=',
rexpr=pgast.ColumnRef(
name=[target_alias, 'source'],
),
),
op='AND',
rexpr=astutils.new_binop(
lexpr=pgast.ColumnRef(
name=[target_alias, 'target'],
),
op='=',
rexpr=pgast.ColumnRef(
name=[data_rvar.alias.aliasname, 'target'],
),
),
),
using_clause=[
dml_cte_rvar,
data_rvar,
],
returning_list=[
pgast.ResTarget(
val=pgast.ColumnRef(
name=[target_alias, pgast.Star()],
),
)
]
)
else:
# Drop all previous link records for this source.
delqry = pgast.DeleteStmt(
relation=target_rvar,
where_clause=astutils.new_binop(
lexpr=source_ref,
op='=',
rexpr=pgast.ColumnRef(
name=[target_alias, 'source'],
),
),
using_clause=[dml_cte_rvar],
returning_list=[
pgast.ResTarget(
val=pgast.ColumnRef(
name=[target_alias, pgast.Star()],
),
)
]
)
delcte = pgast.CommonTableExpr(
name=ctx.env.aliases.get(hint='d'),
query=delqry,
for_dml_stmt=ctx.get_current_dml_stmt(),
)
if shape_op is not qlast.ShapeOp.SUBTRACT:
# Correlate the deletion with INSERT to make sure
# link properties get erased properly and we aren't
# just ON CONFLICT UPDATE-ing the link rows.
# This basically just tacks on a
# WHERE (SELECT count(*) FROM delcte) IS NOT NULL)
del_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
val=pgast.FuncCall(
name=['count'],
args=[pgast.ColumnRef(name=[pgast.Star()])],
),
),
],
from_clause=[
pgast.RelRangeVar(relation=delcte),
],
)
data_select.where_clause = astutils.extend_binop(
data_select.where_clause,
pgast.NullTest(arg=del_select, negated=True),
)
pathctx.put_path_value_rvar(
delcte.query, path_id.ptr_path(), target_rvar
)
# Record the effect of this removal in the relation overlay
# context to ensure that references to the link in the result
# of this DML statement yield the expected results.
relctx.add_ptr_rel_overlay(
mptrref,
context.OverlayOp.EXCEPT,
delcte,
path_id=path_id.ptr_path(),
dml_stmts=ctx.dml_stmt_stack,
ctx=ctx
)
toplevel.append_cte(delcte)
else:
delqry = None
if shape_op is qlast.ShapeOp.SUBTRACT:
if mptrref.dir_cardinality(rptr.direction).can_be_zero():
# The pointer is OPTIONAL, no checks or further processing
# is needed.
return None, None
else:
# The pointer is REQUIRED, so we must take the result of
# the subtraction produced by the "delcte" above, apply it
# as a subtracting overlay, and re-compute the pointer relation
# to see if there are any newly created empty sets.
#
# The actual work is done via raise_on_null injection performed
# by "process_link_values()" below (hence "enforce_cardinality").
#
# The other part of this enforcement is in doing it when a
# target is deleted and the link policy is ALLOW. This is
# handled in _get_outline_link_trigger_proc_text in
# pgsql/delta.py.
# Turn `foo := <expr>` into just `foo`.
ptr_ref_set = irast.Set(
path_id=ir_set.path_id,
path_scope_id=ir_set.path_scope_id,
typeref=ir_set.typeref,
expr=ir_set.expr.replace(expr=None),
)
assert irutils.is_set_instance(ptr_ref_set, irast.Pointer)
with ctx.new() as subctx:
# TODO: Do we really need a copy here? things /seem/
# to work without it
subctx.rel_overlays = subctx.rel_overlays.copy()
relctx.add_ptr_rel_overlay(
ptrref,
context.OverlayOp.EXCEPT,
delcte,
path_id=path_id.ptr_path(),
ctx=subctx
)
check_cte, _ = process_link_values(
ir_stmt=ir_stmt,
ir_expr=ptr_ref_set,
dml_rvar=dml_cte_rvar,
source_typeref=source_typeref,
target_is_scalar=target_is_scalar,
enforce_cardinality=True,
dml_cte=dml_cte,
iterator=iterator,
ctx=subctx,
)
toplevel.append_cte(check_cte)
return None, check_cte
cols = [pgast.ColumnRef(name=[col]) for col in specified_cols]
conflict_cols = ['source', 'target']
if is_insert or target_is_scalar:
conflict_clause = None
elif (
len(cols) == len(conflict_cols)
and delqry is not None
and not policy_ctx
):
# There are no link properties, so we can optimize the
# link replacement operation by omitting the overlapping
# link rows from deletion.
filter_select = pgast.SelectStmt(
target_list=[
pgast.ResTarget(
val=pgast.ColumnRef(name=['source']),
),
pgast.ResTarget(
val=pgast.ColumnRef(name=['target']),
),
],
from_clause=[pgast.RelRangeVar(relation=data_cte)],
)
delqry.where_clause = astutils.extend_binop(
delqry.where_clause,
astutils.new_binop(
lexpr=pgast.ImplicitRowExpr(
args=[
pgast.ColumnRef(name=['source']),
pgast.ColumnRef(name=['target']),
],
),
rexpr=pgast.SubLink(
operator="ALL",
expr=filter_select,
),
op='!=',
)
)
conflict_clause = pgast.OnConflictClause(
action='nothing',
infer=pgast.InferClause(
index_elems=[
pgast.ColumnRef(name=[col]) for col in conflict_cols
]
),
)
else:
# Inserting rows into the link table may produce cardinality
# constraint violations, since the INSERT into the link table
# is executed in the snapshot where the above DELETE from
# the link table is not visible. Hence, we need to use
# the ON CONFLICT clause to resolve this.
conflict_inference = [
pgast.ColumnRef(name=[col])
for col in conflict_cols
]
target_cols = [
col.name[0]
for col in cols
if isinstance(col.name[0], str) and col.name[0] not in conflict_cols
]
if len(target_cols) == 0:
conflict_clause = pgast.OnConflictClause(
action='nothing',
infer=pgast.InferClause(
index_elems=conflict_inference
)
)
else:
conflict_data = pgast.RowExpr(
args=[
pgast.ColumnRef(name=['excluded', col])
for col in target_cols
],
)
conflict_clause = pgast.OnConflictClause(
action='update',
infer=pgast.InferClause(
index_elems=conflict_inference
),
target_list=[
pgast.MultiAssignRef(
columns=target_cols,
source=conflict_data
)
]
)
update = pgast.CommonTableExpr(
name=ctx.env.aliases.get(hint='i'),
query=pgast.InsertStmt(
relation=target_rvar,
select_stmt=data_select,
cols=[
pgast.InsertTarget(name=downcast(str, col.name[0]))
for col in cols
],
on_conflict=conflict_clause,
returning_list=[
pgast.ResTarget(
val=pgast.ColumnRef(name=[pgast.Star()])
)
]
),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
pathctx.put_path_value_rvar(update.query, path_id.ptr_path(), target_rvar)
def register_overlays(
overlay_cte: pgast.CommonTableExpr, octx: context.CompilerContextLevel
) -> None:
assert isinstance(mptrref, irast.PointerRef)
# Record the effect of this insertion in the relation overlay
# context to ensure that references to the link in the result
# of this DML statement yield the expected results.
if shape_op is qlast.ShapeOp.APPEND and not target_is_scalar:
# When doing an UPDATE with +=, we need to do an anti-join
# based filter to filter out links that were already present
# and have been re-added.
relctx.add_ptr_rel_overlay(
mptrref,
context.OverlayOp.FILTER,
overlay_cte,
dml_stmts=ctx.dml_stmt_stack,
path_id=path_id.ptr_path(),
ctx=octx
)
relctx.add_ptr_rel_overlay(
mptrref,
context.OverlayOp.UNION,
overlay_cte,
dml_stmts=ctx.dml_stmt_stack,
path_id=path_id.ptr_path(),
ctx=octx
)
if policy_ctx:
policy_ctx.rel_overlays = policy_ctx.rel_overlays.copy()
register_overlays(data_cte, policy_ctx)
register_overlays(update, ctx)
return update, None | Perform updates to a link relation as part of a DML statement.
Args:
ir_stmt:
IR of the DML statement.
ir_set:
IR of the INSERT/UPDATE body element.
shape_op:
The operation of the UPDATE body element (:=, +=, -=). For
INSERT this should always be :=.
source_typeref:
An ir.TypeRef instance representing the specific type of an object
being updated.
dml_cte:
CTE representing the SQL INSERT or UPDATE to the main
relation of the DML subject.
iterator:
IR and CTE representing the iterator range in the FOR clause
of the EdgeQL DML statement (if present).
policy_ctx:
Optionally, a context in which to populate overlays that
use the select CTE for overlays instead of the
actual insert CTE. This is needed if an access policy is to
be applied, and requires disabling a potential optimization.
We need separate overlay contexts because default values for
link properties don't currently get populated in our IR, so we
need to do actual SQL DML to get their values. (And so we disallow
their use in policies.) | process_link_update | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def process_link_values(
*,
ir_stmt: irast.MutatingStmt,
ir_expr: irast.SetE[irast.Pointer],
dml_rvar: pgast.PathRangeVar,
dml_cte: pgast.CommonTableExpr,
source_typeref: irast.TypeRef,
target_is_scalar: bool,
enforce_cardinality: bool,
iterator: Optional[pgast.IteratorCTE],
ctx: context.CompilerContextLevel,
) -> Tuple[pgast.CommonTableExpr, List[str]]:
"""Produce a pointer relation for a given body element of an INSERT/UPDATE.
Given an INSERT/UPDATE body shape element that mutates a MULTI pointer,
produce a (source, target [, link properties]) relation as a CTE and
return it along with a list of relation attribute names.
Args:
ir_stmt:
IR of the DML statement.
ir_set:
IR of the INSERT/UPDATE body element.
dml_rvar:
The RangeVar over the SQL INSERT/UPDATE of the main relation
of the object being updated.
dml_cte:
CTE representing the SQL INSERT or UPDATE to the main
relation of the DML subject.
source_typeref:
An ir.TypeRef instance representing the specific type of an object
being updated.
target_is_scalar:
True, if mutating a property, False if a link.
enforce_cardinality:
Whether an explicit empty set check should be generated.
Used for REQUIRED pointers.
iterator:
IR and CTE representing the iterator range in the FOR clause
of the EdgeQL DML statement (if present).
Returns:
A tuple containing the pointer relation CTE and a list of attribute
names in it.
"""
old_dml_count = len(ctx.dml_stmts)
with ctx.newrel() as subrelctx:
# For inserts, we need to use the main DML statement as the
# iterator, while for updates, we need to use the DML range
# CTE as the iterator (and so arrange for it to be passed in).
# This is because, for updates, we need to execute any nested
# DML once for each row in the range over all types, while
# dml_cte contains just one subtype.
if isinstance(ir_stmt, irast.InsertStmt):
subrelctx.enclosing_cte_iterator = pgast.IteratorCTE(
path_id=ir_stmt.subject.path_id, cte=dml_cte,
parent=iterator)
else:
subrelctx.enclosing_cte_iterator = iterator
row_query = subrelctx.rel
merge_iterator(iterator, row_query, ctx=subrelctx)
relctx.include_rvar(row_query, dml_rvar, pull_namespace=False,
path_id=ir_stmt.subject.path_id, ctx=subrelctx)
subrelctx.path_scope[ir_stmt.subject.path_id] = row_query
ir_rptr = ir_expr.expr
ptrref = ir_rptr.ptrref
if ptrref.material_ptr is not None:
ptrref = ptrref.material_ptr
assert isinstance(ptrref, irast.PointerRef)
ptr_is_multi_required = (
ptrref.out_cardinality == qltypes.Cardinality.AT_LEAST_ONE
)
with subrelctx.newscope() as sctx, sctx.subrel() as input_rel_ctx:
input_rel = input_rel_ctx.rel
input_rel_ctx.expr_exposed = False
input_rel_ctx.volatility_ref = (
lambda _stmt, _ctx: pathctx.get_path_identity_var(
row_query, ir_stmt.subject.path_id,
env=input_rel_ctx.env),)
# Check if some nested Set provides a shape that is
# visible here.
shape_expr = ir_expr.shape_source or ir_expr
# Register that this shape needs to be compiled for use by DML,
# so that the values will be there for us to grab later.
input_rel_ctx.shapes_needed_by_dml.add(shape_expr)
if ptr_is_multi_required and enforce_cardinality:
input_rel_ctx.force_optional |= {ir_expr.path_id}
dispatch.visit(ir_expr, ctx=input_rel_ctx)
input_stmt: pgast.Query = input_rel
input_rvar = pgast.RangeSubselect(
subquery=input_rel,
lateral=True,
alias=pgast.Alias(
aliasname=ctx.env.aliases.get('val')
)
)
if len(ctx.dml_stmts) > old_dml_count:
# If there were any nested inserts, we need to join them in.
pathctx.put_rvar_path_bond(input_rvar, ir_stmt.subject.path_id)
relctx.include_rvar(row_query, input_rvar,
path_id=ir_stmt.subject.path_id,
ctx=ctx)
source_data: Dict[str, Tuple[irast.PathId, pgast.BaseExpr]] = {}
if isinstance(input_stmt, pgast.SelectStmt) and input_stmt.op is not None:
# UNION
assert input_stmt.rarg
input_stmt = input_stmt.rarg
path_id = ir_expr.path_id
target_ref: pgast.BaseExpr
if shape_expr.shape:
for element, _ in shape_expr.shape:
if not element.path_id.is_linkprop_path():
continue
val = pathctx.get_rvar_path_value_var(
input_rvar, element.path_id, env=ctx.env)
rptr = element.path_id.rptr()
assert isinstance(rptr, irast.PointerRef)
actual_rptr = irtyputils.find_actual_ptrref(source_typeref, rptr)
ptr_info = pg_types.get_ptrref_storage_info(actual_rptr)
real_path_id = path_id.ptr_path().extend(ptrref=actual_rptr)
source_data.setdefault(
ptr_info.column_name, (real_path_id, val))
if not target_is_scalar and 'target' not in source_data:
target_ref = pathctx.get_rvar_path_identity_var(
input_rvar, path_id, env=ctx.env)
else:
if target_is_scalar:
target_ref = pathctx.get_rvar_path_value_var(
input_rvar, path_id, env=ctx.env)
target_ref = output.output_as_value(target_ref, env=ctx.env)
else:
target_ref = pathctx.get_rvar_path_identity_var(
input_rvar, path_id, env=ctx.env)
if isinstance(ir_stmt, irast.UpdateStmt) and not target_is_scalar:
actual_ptrref = irtyputils.find_actual_ptrref(source_typeref, ptrref)
target_ref = check_update_type(
target_ref,
input_rvar,
is_subquery=False,
ir_stmt=ir_stmt,
ir_set=ir_expr,
shape_ptrref=ptrref,
actual_ptrref=actual_ptrref,
ctx=ctx,
)
if ptr_is_multi_required and enforce_cardinality:
target_ref = pgast.FuncCall(
name=astutils.edgedb_func('raise_on_null', ctx=ctx),
args=[
target_ref,
pgast.StringConstant(val='not_null_violation'),
pgast.NamedFuncArg(
name='msg',
val=pgast.StringConstant(val='missing value'),
),
pgast.NamedFuncArg(
name='column',
val=pgast.StringConstant(val=str(ptrref.id)),
),
],
)
source_data['target'] = (path_id, target_ref)
row_query.target_list.append(
pgast.ResTarget(
name='source',
val=pathctx.get_rvar_path_identity_var(
dml_rvar,
ir_stmt.subject.path_id,
env=ctx.env,
),
),
)
specified_cols = ['source']
for col, (col_path_id, expr) in source_data.items():
row_query.target_list.append(
pgast.ResTarget(
val=expr,
name=col,
),
)
specified_cols.append(col)
# XXX: This is dodgy. Do we need to do the dynamic rvar thing?
# XXX: And can we make defaults work?
pathctx._put_path_output_var(
row_query, col_path_id, aspect=pgce.PathAspect.VALUE,
var=pgast.ColumnRef(name=[col]),
)
link_rows = pgast.CommonTableExpr(
query=row_query,
name=ctx.env.aliases.get(hint='r'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)
return link_rows, specified_cols | Produce a pointer relation for a given body element of an INSERT/UPDATE.
Given an INSERT/UPDATE body shape element that mutates a MULTI pointer,
produce a (source, target [, link properties]) relation as a CTE and
return it along with a list of relation attribute names.
Args:
ir_stmt:
IR of the DML statement.
ir_set:
IR of the INSERT/UPDATE body element.
dml_rvar:
The RangeVar over the SQL INSERT/UPDATE of the main relation
of the object being updated.
dml_cte:
CTE representing the SQL INSERT or UPDATE to the main
relation of the DML subject.
source_typeref:
An ir.TypeRef instance representing the specific type of an object
being updated.
target_is_scalar:
True, if mutating a property, False if a link.
enforce_cardinality:
Whether an explicit empty set check should be generated.
Used for REQUIRED pointers.
iterator:
IR and CTE representing the iterator range in the FOR clause
of the EdgeQL DML statement (if present).
Returns:
A tuple containing the pointer relation CTE and a list of attribute
names in it. | process_link_values | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def process_delete_body(
*,
ir_stmt: irast.DeleteStmt,
delete_cte: pgast.CommonTableExpr,
typeref: irast.TypeRef,
ctx: context.CompilerContextLevel,
) -> None:
"""Finalize DELETE on an object.
The actual DELETE was generated in gen_dml_cte, so we only
have work to do here if there are link tables to clean up.
"""
ctx.toplevel_stmt.append_cte(delete_cte)
put_iterator_bond(ctx.enclosing_cte_iterator, delete_cte.query)
pointers = ir_stmt.links_to_delete[typeref.id]
for ptrref in pointers:
target_rvar = relctx.range_for_ptrref(
ptrref, for_mutation=True, only_self=True, ctx=ctx)
assert isinstance(target_rvar, pgast.RelRangeVar)
range_rvar = pgast.RelRangeVar(
relation=delete_cte,
alias=pgast.Alias(
aliasname=ctx.env.aliases.get(hint='range')
)
)
where_clause = astutils.new_binop(
lexpr=pgast.ColumnRef(name=[
target_rvar.alias.aliasname, 'source'
]),
op='=',
rexpr=pathctx.get_rvar_path_identity_var(
range_rvar, ir_stmt.result.path_id, env=ctx.env)
)
del_query = pgast.DeleteStmt(
relation=target_rvar,
where_clause=where_clause,
using_clause=[range_rvar],
)
ctx.toplevel_stmt.append_cte(pgast.CommonTableExpr(
query=del_query,
name=ctx.env.aliases.get(hint='mlink'),
for_dml_stmt=ctx.get_current_dml_stmt(),
)) | Finalize DELETE on an object.
The actual DELETE was generated in gen_dml_cte, so we only
have work to do here if there are link tables to clean up. | process_delete_body | python | geldata/gel | edb/pgsql/compiler/dml.py | https://github.com/geldata/gel/blob/master/edb/pgsql/compiler/dml.py | Apache-2.0 |
def __init__(self, uri):
"""
Create a fake Urllib2 request.
@param uri: Request URI.
@type uri: L{bytes}
"""
self.uri = nativeString(uri)
self.headers = Headers()
_uri = URI.fromBytes(uri)
self.type = nativeString(_uri.scheme)
self.host = nativeString(_uri.host)
if (_uri.scheme, _uri.port) not in ((b'http', 80), (b'https', 443)):
# If it's not a schema on the regular port, add the port.
self.host += ":" + str(_uri.port)
if _PY3:
self.origin_req_host = nativeString(_uri.host)
self.unverifiable = lambda _: False | Create a fake Urllib2 request.
@param uri: Request URI.
@type uri: L{bytes} | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def request(self, method, uri, headers=None, bodyProducer=None):
"""
Issue a new request to the wrapped L{Agent}.
Send a I{Cookie} header if a cookie for C{uri} is stored in
L{CookieAgent.cookieJar}. Cookies are automatically extracted and
stored from requests.
If a C{'cookie'} header appears in C{headers} it will override the
automatic cookie header obtained from the cookie jar.
@see: L{Agent.request}
"""
if headers is None:
headers = Headers()
lastRequest = _FakeUrllib2Request(uri)
# Setting a cookie header explicitly will disable automatic request
# cookies.
if not headers.hasHeader(b'cookie'):
self.cookieJar.add_cookie_header(lastRequest)
cookieHeader = lastRequest.get_header('Cookie', None)
if cookieHeader is not None:
headers = headers.copy()
headers.addRawHeader(b'cookie', networkString(cookieHeader))
d = self._agent.request(method, uri, headers, bodyProducer)
d.addCallback(self._extractCookies, lastRequest)
return d | Issue a new request to the wrapped L{Agent}.
Send a I{Cookie} header if a cookie for C{uri} is stored in
L{CookieAgent.cookieJar}. Cookies are automatically extracted and
stored from requests.
If a C{'cookie'} header appears in C{headers} it will override the
automatic cookie header obtained from the cookie jar.
@see: L{Agent.request} | request | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def _extractCookies(self, response, request):
"""
Extract response cookies and store them in the cookie jar.
@type response: L{twisted.web.iweb.IResponse}
@param response: Twisted Web response.
@param request: A urllib2 compatible request object.
"""
resp = _FakeUrllib2Response(response)
self.cookieJar.extract_cookies(resp, request)
return response | Extract response cookies and store them in the cookie jar.
@type response: L{twisted.web.iweb.IResponse}
@param response: Twisted Web response.
@param request: A urllib2 compatible request object. | _extractCookies | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def deliverBody(self, protocol):
"""
Override C{deliverBody} to wrap the given C{protocol} with
L{_GzipProtocol}.
"""
self.original.deliverBody(_GzipProtocol(protocol, self.original)) | Override C{deliverBody} to wrap the given C{protocol} with
L{_GzipProtocol}. | deliverBody | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def dataReceived(self, data):
"""
Decompress C{data} with the zlib decompressor, forwarding the raw data
to the original protocol.
"""
try:
rawData = self._zlibDecompress.decompress(data)
except zlib.error:
raise ResponseFailed([Failure()], self._response)
if rawData:
self.original.dataReceived(rawData) | Decompress C{data} with the zlib decompressor, forwarding the raw data
to the original protocol. | dataReceived | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def connectionLost(self, reason):
"""
Forward the connection lost event, flushing remaining data from the
decompressor if any.
"""
try:
rawData = self._zlibDecompress.flush()
except zlib.error:
raise ResponseFailed([reason, Failure()], self._response)
if rawData:
self.original.dataReceived(rawData)
self.original.connectionLost(reason) | Forward the connection lost event, flushing remaining data from the
decompressor if any. | connectionLost | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def request(self, method, uri, headers=None, bodyProducer=None):
"""
Send a client request which declares supporting compressed content.
@see: L{Agent.request}.
"""
if headers is None:
headers = Headers()
else:
headers = headers.copy()
headers.addRawHeader(b'accept-encoding', self._supported)
deferred = self._agent.request(method, uri, headers, bodyProducer)
return deferred.addCallback(self._handleResponse) | Send a client request which declares supporting compressed content.
@see: L{Agent.request}. | request | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def _handleResponse(self, response):
"""
Check if the response is encoded, and wrap it to handle decompression.
"""
contentEncodingHeaders = response.headers.getRawHeaders(
b'content-encoding', [])
contentEncodingHeaders = b','.join(contentEncodingHeaders).split(b',')
while contentEncodingHeaders:
name = contentEncodingHeaders.pop().strip()
decoder = self._decoders.get(name)
if decoder is not None:
response = decoder(response)
else:
# Add it back
contentEncodingHeaders.append(name)
break
if contentEncodingHeaders:
response.headers.setRawHeaders(
b'content-encoding', [b','.join(contentEncodingHeaders)])
else:
response.headers.removeHeader(b'content-encoding')
return response | Check if the response is encoded, and wrap it to handle decompression. | _handleResponse | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def request(self, method, uri, headers=None, bodyProducer=None):
"""
Send a client request following HTTP redirects.
@see: L{Agent.request}.
"""
deferred = self._agent.request(method, uri, headers, bodyProducer)
return deferred.addCallback(
self._handleResponse, method, uri, headers, 0) | Send a client request following HTTP redirects.
@see: L{Agent.request}. | request | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def _resolveLocation(self, requestURI, location):
"""
Resolve the redirect location against the request I{URI}.
@type requestURI: C{bytes}
@param requestURI: The request I{URI}.
@type location: C{bytes}
@param location: The redirect location.
@rtype: C{bytes}
@return: Final resolved I{URI}.
"""
return _urljoin(requestURI, location) | Resolve the redirect location against the request I{URI}.
@type requestURI: C{bytes}
@param requestURI: The request I{URI}.
@type location: C{bytes}
@param location: The redirect location.
@rtype: C{bytes}
@return: Final resolved I{URI}. | _resolveLocation | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def _handleRedirect(self, response, method, uri, headers, redirectCount):
"""
Handle a redirect response, checking the number of redirects already
followed, and extracting the location header fields.
"""
if redirectCount >= self._redirectLimit:
err = error.InfiniteRedirection(
response.code,
b'Infinite redirection detected',
location=uri)
raise ResponseFailed([Failure(err)], response)
locationHeaders = response.headers.getRawHeaders(b'location', [])
if not locationHeaders:
err = error.RedirectWithNoLocation(
response.code, b'No location header field', uri)
raise ResponseFailed([Failure(err)], response)
location = self._resolveLocation(uri, locationHeaders[0])
deferred = self._agent.request(method, location, headers)
def _chainResponse(newResponse):
newResponse.setPreviousResponse(response)
return newResponse
deferred.addCallback(_chainResponse)
return deferred.addCallback(
self._handleResponse, method, uri, headers, redirectCount + 1) | Handle a redirect response, checking the number of redirects already
followed, and extracting the location header fields. | _handleRedirect | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def _handleResponse(self, response, method, uri, headers, redirectCount):
"""
Handle the response, making another request if it indicates a redirect.
"""
if response.code in self._redirectResponses:
if method not in (b'GET', b'HEAD'):
err = error.PageRedirect(response.code, location=uri)
raise ResponseFailed([Failure(err)], response)
return self._handleRedirect(response, method, uri, headers,
redirectCount)
elif response.code in self._seeOtherResponses:
return self._handleRedirect(response, b'GET', uri, headers,
redirectCount)
return response | Handle the response, making another request if it indicates a redirect. | _handleResponse | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def __init__(self, status, message, deferred):
"""
@param status: Status of L{IResponse}
@ivar status: L{int}
@param message: Message of L{IResponse}
@type message: L{bytes}
@param deferred: deferred to fire when response is complete
@type deferred: L{Deferred} firing with L{bytes}
"""
self.deferred = deferred
self.status = status
self.message = message
self.dataBuffer = [] | @param status: Status of L{IResponse}
@ivar status: L{int}
@param message: Message of L{IResponse}
@type message: L{bytes}
@param deferred: deferred to fire when response is complete
@type deferred: L{Deferred} firing with L{bytes} | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def dataReceived(self, data):
"""
Accumulate some more bytes from the response.
"""
self.dataBuffer.append(data) | Accumulate some more bytes from the response. | dataReceived | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def connectionLost(self, reason):
"""
Deliver the accumulated response bytes to the waiting L{Deferred}, if
the response body has been completely received without error.
"""
if reason.check(ResponseDone):
self.deferred.callback(b''.join(self.dataBuffer))
elif reason.check(PotentialDataLoss):
self.deferred.errback(
PartialDownloadError(self.status, self.message,
b''.join(self.dataBuffer)))
else:
self.deferred.errback(reason) | Deliver the accumulated response bytes to the waiting L{Deferred}, if
the response body has been completely received without error. | connectionLost | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def cancel(deferred):
"""
Cancel a L{readBody} call, close the connection to the HTTP server
immediately, if it is still open.
@param deferred: The cancelled L{defer.Deferred}.
"""
abort = getAbort()
if abort is not None:
abort() | Cancel a L{readBody} call, close the connection to the HTTP server
immediately, if it is still open.
@param deferred: The cancelled L{defer.Deferred}. | readBody.cancel | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def readBody(response):
"""
Get the body of an L{IResponse} and return it as a byte string.
This is a helper function for clients that don't want to incrementally
receive the body of an HTTP response.
@param response: The HTTP response for which the body will be read.
@type response: L{IResponse} provider
@return: A L{Deferred} which will fire with the body of the response.
Cancelling it will close the connection to the server immediately.
"""
def cancel(deferred):
"""
Cancel a L{readBody} call, close the connection to the HTTP server
immediately, if it is still open.
@param deferred: The cancelled L{defer.Deferred}.
"""
abort = getAbort()
if abort is not None:
abort()
d = defer.Deferred(cancel)
protocol = _ReadBodyProtocol(response.code, response.phrase, d)
def getAbort():
return getattr(protocol.transport, 'abortConnection', None)
response.deliverBody(protocol)
if protocol.transport is not None and getAbort() is None:
warnings.warn(
'Using readBody with a transport that does not have an '
'abortConnection method',
category=DeprecationWarning,
stacklevel=2)
return d | Get the body of an L{IResponse} and return it as a byte string.
This is a helper function for clients that don't want to incrementally
receive the body of an HTTP response.
@param response: The HTTP response for which the body will be read.
@type response: L{IResponse} provider
@return: A L{Deferred} which will fire with the body of the response.
Cancelling it will close the connection to the server immediately. | readBody | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/client.py | MIT |
def saveMark(self):
'''Get the line number and column of the last character parsed'''
# This gets replaced during dataReceived, restored afterwards
return (self.lineno, self.colno) | Get the line number and column of the last character parsed | saveMark | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def _buildStateTable(self):
'''Return a dictionary of begin, do, end state function tuples'''
# _buildStateTable leaves something to be desired but it does what it
# does.. probably slowly, so I'm doing some evil caching so it doesn't
# get called more than once per class.
stateTable = getattr(self.__class__, '__stateTable', None)
if stateTable is None:
stateTable = self.__class__.__stateTable = zipfndict(
*[prefixedMethodObjDict(self, prefix)
for prefix in ('begin_', 'do_', 'end_')])
return stateTable | Return a dictionary of begin, do, end state function tuples | _buildStateTable | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def connectionLost(self, reason):
"""
End the last state we were in.
"""
stateTable = self._buildStateTable()
stateTable[self.state][END_HANDLER]() | End the last state we were in. | connectionLost | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotTagStart(self, name, attributes):
'''Encountered an opening tag.
Default behaviour is to print.'''
print('begin', name, attributes) | Encountered an opening tag.
Default behaviour is to print. | gotTagStart | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotText(self, data):
'''Encountered text
Default behaviour is to print.'''
print('text:', repr(data)) | Encountered text
Default behaviour is to print. | gotText | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotEntityReference(self, entityRef):
'''Encountered mnemonic entity reference
Default behaviour is to print.'''
print('entityRef: &%s;' % entityRef) | Encountered mnemonic entity reference
Default behaviour is to print. | gotEntityReference | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotComment(self, comment):
'''Encountered comment.
Default behaviour is to ignore.'''
pass | Encountered comment.
Default behaviour is to ignore. | gotComment | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotCData(self, cdata):
'''Encountered CDATA
Default behaviour is to call the gotText method'''
self.gotText(cdata) | Encountered CDATA
Default behaviour is to call the gotText method | gotCData | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotDoctype(self, doctype):
"""Encountered DOCTYPE
This is really grotty: it basically just gives you everything between
'<!DOCTYPE' and '>' as an argument.
"""
print('!DOCTYPE', repr(doctype)) | Encountered DOCTYPE
This is really grotty: it basically just gives you everything between
'<!DOCTYPE' and '>' as an argument. | gotDoctype | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def gotTagEnd(self, name):
'''Encountered closing tag
Default behaviour is to print.'''
print('end', name) | Encountered closing tag
Default behaviour is to print. | gotTagEnd | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/sux.py | MIT |
def output(func, *args, **kw):
"""output(func, *args, **kw) -> html string
Either return the result of a function (which presumably returns an
HTML-legal string) or a sparse HTMLized error message and a message
in the server log.
"""
try:
return func(*args, **kw)
except:
log.msg("Error calling %r:" % (func,))
log.err()
return PRE("An error occurred.") | output(func, *args, **kw) -> html string
Either return the result of a function (which presumably returns an
HTML-legal string) or a sparse HTMLized error message and a message
in the server log. | output | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/html.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/html.py | MIT |
def __init__(self, data, type):
"""
@param data: The bytes that make up this data resource.
@type data: L{bytes}
@param type: A native string giving the Internet media type for this
content.
@type type: L{str}
"""
resource.Resource.__init__(self)
self.data = data
self.type = type | @param data: The bytes that make up this data resource.
@type data: L{bytes}
@param type: A native string giving the Internet media type for this
content.
@type type: L{str} | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def addSlash(request):
"""
Add a trailing slash to C{request}'s URI. Deprecated, do not use.
"""
return _addSlash(request) | Add a trailing slash to C{request}'s URI. Deprecated, do not use. | addSlash | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _addSlash(request):
"""
Add a trailing slash to C{request}'s URI.
@param request: The incoming request to add the ending slash to.
@type request: An object conforming to L{twisted.web.iweb.IRequest}
@return: A URI with a trailing slash, with query and fragment preserved.
@rtype: L{bytes}
"""
url = URL.fromText(request.uri.decode('ascii'))
# Add an empty path segment at the end, so that it adds a trailing slash
url = url.replace(path=list(url.path) + [u""])
return url.asText().encode('ascii') | Add a trailing slash to C{request}'s URI.
@param request: The incoming request to add the ending slash to.
@type request: An object conforming to L{twisted.web.iweb.IRequest}
@return: A URI with a trailing slash, with query and fragment preserved.
@rtype: L{bytes} | _addSlash | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def loadMimeTypes(mimetype_locations=None, init=mimetypes.init):
"""
Produces a mapping of extensions (with leading dot) to MIME types.
It does this by calling the C{init} function of the L{mimetypes} module.
This will have the side effect of modifying the global MIME types cache
in that module.
Multiple file locations containing mime-types can be passed as a list.
The files will be sourced in that order, overriding mime-types from the
files sourced beforehand, but only if a new entry explicitly overrides
the current entry.
@param mimetype_locations: Optional. List of paths to C{mime.types} style
files that should be used.
@type mimetype_locations: iterable of paths or L{None}
@param init: The init function to call. Defaults to the global C{init}
function of the C{mimetypes} module. For internal use (testing) only.
@type init: callable
"""
init(mimetype_locations)
mimetypes.types_map.update(
{
'.conf': 'text/plain',
'.diff': 'text/plain',
'.flac': 'audio/x-flac',
'.java': 'text/plain',
'.oz': 'text/x-oz',
'.swf': 'application/x-shockwave-flash',
'.wml': 'text/vnd.wap.wml',
'.xul': 'application/vnd.mozilla.xul+xml',
'.patch': 'text/plain'
}
)
return mimetypes.types_map | Produces a mapping of extensions (with leading dot) to MIME types.
It does this by calling the C{init} function of the L{mimetypes} module.
This will have the side effect of modifying the global MIME types cache
in that module.
Multiple file locations containing mime-types can be passed as a list.
The files will be sourced in that order, overriding mime-types from the
files sourced beforehand, but only if a new entry explicitly overrides
the current entry.
@param mimetype_locations: Optional. List of paths to C{mime.types} style
files that should be used.
@type mimetype_locations: iterable of paths or L{None}
@param init: The init function to call. Defaults to the global C{init}
function of the C{mimetypes} module. For internal use (testing) only.
@type init: callable | loadMimeTypes | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def __init__(self, path, defaultType="text/html", ignoredExts=(), registry=None, allowExt=0):
"""
Create a file with the given path.
@param path: The filename of the file from which this L{File} will
serve data.
@type path: C{str}
@param defaultType: A I{major/minor}-style MIME type specifier
indicating the I{Content-Type} with which this L{File}'s data
will be served if a MIME type cannot be determined based on
C{path}'s extension.
@type defaultType: C{str}
@param ignoredExts: A sequence giving the extensions of paths in the
filesystem which will be ignored for the purposes of child
lookup. For example, if C{ignoredExts} is C{(".bar",)} and
C{path} is a directory containing a file named C{"foo.bar"}, a
request for the C{"foo"} child of this resource will succeed
with a L{File} pointing to C{"foo.bar"}.
@param registry: The registry object being used to handle this
request. If L{None}, one will be created.
@type registry: L{Registry}
@param allowExt: Ignored parameter, only present for backwards
compatibility. Do not pass a value for this parameter.
"""
resource.Resource.__init__(self)
filepath.FilePath.__init__(self, path)
self.defaultType = defaultType
if ignoredExts in (0, 1) or allowExt:
warnings.warn("ignoredExts should receive a list, not a boolean")
if ignoredExts or allowExt:
self.ignoredExts = ['*']
else:
self.ignoredExts = []
else:
self.ignoredExts = list(ignoredExts)
self.registry = registry or Registry() | Create a file with the given path.
@param path: The filename of the file from which this L{File} will
serve data.
@type path: C{str}
@param defaultType: A I{major/minor}-style MIME type specifier
indicating the I{Content-Type} with which this L{File}'s data
will be served if a MIME type cannot be determined based on
C{path}'s extension.
@type defaultType: C{str}
@param ignoredExts: A sequence giving the extensions of paths in the
filesystem which will be ignored for the purposes of child
lookup. For example, if C{ignoredExts} is C{(".bar",)} and
C{path} is a directory containing a file named C{"foo.bar"}, a
request for the C{"foo"} child of this resource will succeed
with a L{File} pointing to C{"foo.bar"}.
@param registry: The registry object being used to handle this
request. If L{None}, one will be created.
@type registry: L{Registry}
@param allowExt: Ignored parameter, only present for backwards
compatibility. Do not pass a value for this parameter. | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def directoryListing(self):
"""
Return a resource that generates an HTML listing of the
directory this path represents.
@return: A resource that renders the directory to HTML.
@rtype: L{DirectoryLister}
"""
if _PY3:
path = self.path
names = self.listNames()
else:
# DirectoryLister works in terms of native strings, so on
# Python 2, ensure we have a bytes paths for this
# directory and its contents. We use the asBytesMode
# method inherited from FilePath to ensure consistent
# encoding of the actual path. This returns a FilePath
# instance even when called on subclasses, however, so we
# have to create a new File instance.
nativeStringPath = self.createSimilarFile(self.asBytesMode().path)
path = nativeStringPath.path
names = nativeStringPath.listNames()
return DirectoryLister(path,
names,
self.contentTypes,
self.contentEncodings,
self.defaultType) | Return a resource that generates an HTML listing of the
directory this path represents.
@return: A resource that renders the directory to HTML.
@rtype: L{DirectoryLister} | directoryListing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def getChild(self, path, request):
"""
If this L{File}"s path refers to a directory, return a L{File}
referring to the file named C{path} in that directory.
If C{path} is the empty string, return a L{DirectoryLister}
instead.
@param path: The current path segment.
@type path: L{bytes}
@param request: The incoming request.
@type request: An that provides L{twisted.web.iweb.IRequest}.
@return: A resource representing the requested file or
directory, or L{NoResource} if the path cannot be
accessed.
@rtype: An object that provides L{resource.IResource}.
"""
if isinstance(path, bytes):
try:
# Request calls urllib.unquote on each path segment,
# leaving us with raw bytes.
path = path.decode('utf-8')
except UnicodeDecodeError:
log.err(None,
"Could not decode path segment as utf-8: %r" % (path,))
return self.childNotFound
self.restat(reraise=False)
if not self.isdir():
return self.childNotFound
if path:
try:
fpath = self.child(path)
except filepath.InsecurePath:
return self.childNotFound
else:
fpath = self.childSearchPreauth(*self.indexNames)
if fpath is None:
return self.directoryListing()
if not fpath.exists():
fpath = fpath.siblingExtensionSearch(*self.ignoredExts)
if fpath is None:
return self.childNotFound
extension = fpath.splitext()[1]
if platformType == "win32":
# don't want .RPY to be different than .rpy, since that would allow
# source disclosure.
processor = InsensitiveDict(self.processors).get(extension)
else:
processor = self.processors.get(extension)
if processor:
return resource.IResource(processor(fpath.path, self.registry))
return self.createSimilarFile(fpath.path) | If this L{File}"s path refers to a directory, return a L{File}
referring to the file named C{path} in that directory.
If C{path} is the empty string, return a L{DirectoryLister}
instead.
@param path: The current path segment.
@type path: L{bytes}
@param request: The incoming request.
@type request: An that provides L{twisted.web.iweb.IRequest}.
@return: A resource representing the requested file or
directory, or L{NoResource} if the path cannot be
accessed.
@rtype: An object that provides L{resource.IResource}. | getChild | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def openForReading(self):
"""Open a file and return it."""
return self.open() | Open a file and return it. | openForReading | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def getFileSize(self):
"""Return file size."""
return self.getsize() | Return file size. | getFileSize | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _parseRangeHeader(self, range):
"""
Parse the value of a Range header into (start, stop) pairs.
In a given pair, either of start or stop can be None, signifying that
no value was provided, but not both.
@return: A list C{[(start, stop)]} of pairs of length at least one.
@raise ValueError: if the header is syntactically invalid or if the
Bytes-Unit is anything other than "bytes'.
"""
try:
kind, value = range.split(b'=', 1)
except ValueError:
raise ValueError("Missing '=' separator")
kind = kind.strip()
if kind != b'bytes':
raise ValueError("Unsupported Bytes-Unit: %r" % (kind,))
unparsedRanges = list(filter(None, map(bytes.strip, value.split(b','))))
parsedRanges = []
for byteRange in unparsedRanges:
try:
start, end = byteRange.split(b'-', 1)
except ValueError:
raise ValueError("Invalid Byte-Range: %r" % (byteRange,))
if start:
try:
start = int(start)
except ValueError:
raise ValueError("Invalid Byte-Range: %r" % (byteRange,))
else:
start = None
if end:
try:
end = int(end)
except ValueError:
raise ValueError("Invalid Byte-Range: %r" % (byteRange,))
else:
end = None
if start is not None:
if end is not None and start > end:
# Start must be less than or equal to end or it is invalid.
raise ValueError("Invalid Byte-Range: %r" % (byteRange,))
elif end is None:
# One or both of start and end must be specified. Omitting
# both is invalid.
raise ValueError("Invalid Byte-Range: %r" % (byteRange,))
parsedRanges.append((start, end))
return parsedRanges | Parse the value of a Range header into (start, stop) pairs.
In a given pair, either of start or stop can be None, signifying that
no value was provided, but not both.
@return: A list C{[(start, stop)]} of pairs of length at least one.
@raise ValueError: if the header is syntactically invalid or if the
Bytes-Unit is anything other than "bytes'. | _parseRangeHeader | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _rangeToOffsetAndSize(self, start, end):
"""
Convert a start and end from a Range header to an offset and size.
This method checks that the resulting range overlaps with the resource
being served (and so has the value of C{getFileSize()} as an indirect
input).
Either but not both of start or end can be L{None}:
- Omitted start means that the end value is actually a start value
relative to the end of the resource.
- Omitted end means the end of the resource should be the end of
the range.
End is interpreted as inclusive, as per RFC 2616.
If this range doesn't overlap with any of this resource, C{(0, 0)} is
returned, which is not otherwise a value return value.
@param start: The start value from the header, or L{None} if one was
not present.
@param end: The end value from the header, or L{None} if one was not
present.
@return: C{(offset, size)} where offset is how far into this resource
this resource the range begins and size is how long the range is,
or C{(0, 0)} if the range does not overlap this resource.
"""
size = self.getFileSize()
if start is None:
start = size - end
end = size
elif end is None:
end = size
elif end < size:
end += 1
elif end > size:
end = size
if start >= size:
start = end = 0
return start, (end - start) | Convert a start and end from a Range header to an offset and size.
This method checks that the resulting range overlaps with the resource
being served (and so has the value of C{getFileSize()} as an indirect
input).
Either but not both of start or end can be L{None}:
- Omitted start means that the end value is actually a start value
relative to the end of the resource.
- Omitted end means the end of the resource should be the end of
the range.
End is interpreted as inclusive, as per RFC 2616.
If this range doesn't overlap with any of this resource, C{(0, 0)} is
returned, which is not otherwise a value return value.
@param start: The start value from the header, or L{None} if one was
not present.
@param end: The end value from the header, or L{None} if one was not
present.
@return: C{(offset, size)} where offset is how far into this resource
this resource the range begins and size is how long the range is,
or C{(0, 0)} if the range does not overlap this resource. | _rangeToOffsetAndSize | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _contentRange(self, offset, size):
"""
Return a string suitable for the value of a Content-Range header for a
range with the given offset and size.
The offset and size are not sanity checked in any way.
@param offset: How far into this resource the range begins.
@param size: How long the range is.
@return: The value as appropriate for the value of a Content-Range
header.
"""
return networkString('bytes %d-%d/%d' % (
offset, offset + size - 1, self.getFileSize())) | Return a string suitable for the value of a Content-Range header for a
range with the given offset and size.
The offset and size are not sanity checked in any way.
@param offset: How far into this resource the range begins.
@param size: How long the range is.
@return: The value as appropriate for the value of a Content-Range
header. | _contentRange | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _doSingleRangeRequest(self, request, startAndEnd):
"""
Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response
code and Content-Range header appropriately. The return value
indicates which part of the resource to return.
@param request: The Request object.
@param startAndEnd: A 2-tuple of start of the byte range as specified by
the header and the end of the byte range as specified by the header.
At most one of the start and end may be L{None}.
@return: A 2-tuple of the offset and size of the range to return.
offset == size == 0 indicates that the request is not satisfiable.
"""
start, end = startAndEnd
offset, size = self._rangeToOffsetAndSize(start, end)
if offset == size == 0:
# This range doesn't overlap with any of this resource, so the
# request is unsatisfiable.
request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
request.setHeader(
b'content-range', networkString('bytes */%d' % (self.getFileSize(),)))
else:
request.setResponseCode(http.PARTIAL_CONTENT)
request.setHeader(
b'content-range', self._contentRange(offset, size))
return offset, size | Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response
code and Content-Range header appropriately. The return value
indicates which part of the resource to return.
@param request: The Request object.
@param startAndEnd: A 2-tuple of start of the byte range as specified by
the header and the end of the byte range as specified by the header.
At most one of the start and end may be L{None}.
@return: A 2-tuple of the offset and size of the range to return.
offset == size == 0 indicates that the request is not satisfiable. | _doSingleRangeRequest | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _doMultipleRangeRequest(self, request, byteRanges):
"""
Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response
code and Content-Type and Content-Length headers appropriately. The
return value, which is a little complicated, indicates which parts of
the resource to return and the boundaries that should separate the
parts.
In detail, the return value is a tuple rangeInfo C{rangeInfo} is a
list of 3-tuples C{(partSeparator, partOffset, partSize)}. The
response to this request should be, for each element of C{rangeInfo},
C{partSeparator} followed by C{partSize} bytes of the resource
starting at C{partOffset}. Each C{partSeparator} includes the
MIME-style boundary and the part-specific Content-type and
Content-range headers. It is convenient to return the separator as a
concrete string from this method, because this method needs to compute
the number of bytes that will make up the response to be able to set
the Content-Length header of the response accurately.
@param request: The Request object.
@param byteRanges: A list of C{(start, end)} values as specified by
the header. For each range, at most one of C{start} and C{end}
may be L{None}.
@return: See above.
"""
matchingRangeFound = False
rangeInfo = []
contentLength = 0
boundary = networkString("%x%x" % (int(time.time()*1000000), os.getpid()))
if self.type:
contentType = self.type
else:
contentType = b'bytes' # It's what Apache does...
for start, end in byteRanges:
partOffset, partSize = self._rangeToOffsetAndSize(start, end)
if partOffset == partSize == 0:
continue
contentLength += partSize
matchingRangeFound = True
partContentRange = self._contentRange(partOffset, partSize)
partSeparator = networkString((
"\r\n"
"--%s\r\n"
"Content-type: %s\r\n"
"Content-range: %s\r\n"
"\r\n") % (nativeString(boundary), nativeString(contentType), nativeString(partContentRange)))
contentLength += len(partSeparator)
rangeInfo.append((partSeparator, partOffset, partSize))
if not matchingRangeFound:
request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
request.setHeader(
b'content-length', b'0')
request.setHeader(
b'content-range', networkString('bytes */%d' % (self.getFileSize(),)))
return [], b''
finalBoundary = b"\r\n--" + boundary + b"--\r\n"
rangeInfo.append((finalBoundary, 0, 0))
request.setResponseCode(http.PARTIAL_CONTENT)
request.setHeader(
b'content-type', networkString('multipart/byteranges; boundary="%s"' % (nativeString(boundary),)))
request.setHeader(
b'content-length', intToBytes(contentLength + len(finalBoundary)))
return rangeInfo | Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response
code and Content-Type and Content-Length headers appropriately. The
return value, which is a little complicated, indicates which parts of
the resource to return and the boundaries that should separate the
parts.
In detail, the return value is a tuple rangeInfo C{rangeInfo} is a
list of 3-tuples C{(partSeparator, partOffset, partSize)}. The
response to this request should be, for each element of C{rangeInfo},
C{partSeparator} followed by C{partSize} bytes of the resource
starting at C{partOffset}. Each C{partSeparator} includes the
MIME-style boundary and the part-specific Content-type and
Content-range headers. It is convenient to return the separator as a
concrete string from this method, because this method needs to compute
the number of bytes that will make up the response to be able to set
the Content-Length header of the response accurately.
@param request: The Request object.
@param byteRanges: A list of C{(start, end)} values as specified by
the header. For each range, at most one of C{start} and C{end}
may be L{None}.
@return: See above. | _doMultipleRangeRequest | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _setContentHeaders(self, request, size=None):
"""
Set the Content-length and Content-type headers for this request.
This method is not appropriate for requests for multiple byte ranges;
L{_doMultipleRangeRequest} will set these headers in that case.
@param request: The L{twisted.web.http.Request} object.
@param size: The size of the response. If not specified, default to
C{self.getFileSize()}.
"""
if size is None:
size = self.getFileSize()
request.setHeader(b'content-length', intToBytes(size))
if self.type:
request.setHeader(b'content-type', networkString(self.type))
if self.encoding:
request.setHeader(b'content-encoding', networkString(self.encoding)) | Set the Content-length and Content-type headers for this request.
This method is not appropriate for requests for multiple byte ranges;
L{_doMultipleRangeRequest} will set these headers in that case.
@param request: The L{twisted.web.http.Request} object.
@param size: The size of the response. If not specified, default to
C{self.getFileSize()}. | _setContentHeaders | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def makeProducer(self, request, fileForReading):
"""
Make a L{StaticProducer} that will produce the body of this response.
This method will also set the response code and Content-* headers.
@param request: The L{twisted.web.http.Request} object.
@param fileForReading: The file object containing the resource.
@return: A L{StaticProducer}. Calling C{.start()} on this will begin
producing the response.
"""
byteRange = request.getHeader(b'range')
if byteRange is None:
self._setContentHeaders(request)
request.setResponseCode(http.OK)
return NoRangeStaticProducer(request, fileForReading)
try:
parsedRanges = self._parseRangeHeader(byteRange)
except ValueError:
log.msg("Ignoring malformed Range header %r" % (byteRange.decode(),))
self._setContentHeaders(request)
request.setResponseCode(http.OK)
return NoRangeStaticProducer(request, fileForReading)
if len(parsedRanges) == 1:
offset, size = self._doSingleRangeRequest(
request, parsedRanges[0])
self._setContentHeaders(request, size)
return SingleRangeStaticProducer(
request, fileForReading, offset, size)
else:
rangeInfo = self._doMultipleRangeRequest(request, parsedRanges)
return MultipleRangeStaticProducer(
request, fileForReading, rangeInfo) | Make a L{StaticProducer} that will produce the body of this response.
This method will also set the response code and Content-* headers.
@param request: The L{twisted.web.http.Request} object.
@param fileForReading: The file object containing the resource.
@return: A L{StaticProducer}. Calling C{.start()} on this will begin
producing the response. | makeProducer | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def render_GET(self, request):
"""
Begin sending the contents of this L{File} (or a subset of the
contents, based on the 'range' header) to the given request.
"""
self.restat(False)
if self.type is None:
self.type, self.encoding = getTypeAndEncoding(self.basename(),
self.contentTypes,
self.contentEncodings,
self.defaultType)
if not self.exists():
return self.childNotFound.render(request)
if self.isdir():
return self.redirect(request)
request.setHeader(b'accept-ranges', b'bytes')
try:
fileForReading = self.openForReading()
except IOError as e:
if e.errno == errno.EACCES:
return self.forbidden.render(request)
else:
raise
if request.setLastModified(self.getModificationTime()) is http.CACHED:
# `setLastModified` also sets the response code for us, so if the
# request is cached, we close the file now that we've made sure that
# the request would otherwise succeed and return an empty body.
fileForReading.close()
return b''
if request.method == b'HEAD':
# Set the content headers here, rather than making a producer.
self._setContentHeaders(request)
# We've opened the file to make sure it's accessible, so close it
# now that we don't need it.
fileForReading.close()
return b''
producer = self.makeProducer(request, fileForReading)
producer.start()
# and make sure the connection doesn't get closed
return server.NOT_DONE_YET | Begin sending the contents of this L{File} (or a subset of the
contents, based on the 'range' header) to the given request. | render_GET | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def stopProducing(self):
"""
Stop producing data.
L{twisted.internet.interfaces.IProducer.stopProducing}
is called when our consumer has died, and subclasses also call this
method when they are done producing data.
"""
self.fileObject.close()
self.request = None | Stop producing data.
L{twisted.internet.interfaces.IProducer.stopProducing}
is called when our consumer has died, and subclasses also call this
method when they are done producing data. | stopProducing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def __init__(self, request, fileObject, offset, size):
"""
Initialize the instance.
@param request: See L{StaticProducer}.
@param fileObject: See L{StaticProducer}.
@param offset: The offset into the file of the chunk to be written.
@param size: The size of the chunk to write.
"""
StaticProducer.__init__(self, request, fileObject)
self.offset = offset
self.size = size | Initialize the instance.
@param request: See L{StaticProducer}.
@param fileObject: See L{StaticProducer}.
@param offset: The offset into the file of the chunk to be written.
@param size: The size of the chunk to write. | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def __init__(self, request, fileObject, rangeInfo):
"""
Initialize the instance.
@param request: See L{StaticProducer}.
@param fileObject: See L{StaticProducer}.
@param rangeInfo: A list of tuples C{[(boundary, offset, size)]}
where:
- C{boundary} will be written to the request first.
- C{offset} the offset into the file of chunk to write.
- C{size} the size of the chunk to write.
"""
StaticProducer.__init__(self, request, fileObject)
self.rangeInfo = rangeInfo | Initialize the instance.
@param request: See L{StaticProducer}.
@param fileObject: See L{StaticProducer}.
@param rangeInfo: A list of tuples C{[(boundary, offset, size)]}
where:
- C{boundary} will be written to the request first.
- C{offset} the offset into the file of chunk to write.
- C{size} the size of the chunk to write. | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def formatFileSize(size):
"""
Format the given file size in bytes to human readable format.
"""
if size < 1024:
return '%iB' % size
elif size < (1024 ** 2):
return '%iK' % (size / 1024)
elif size < (1024 ** 3):
return '%iM' % (size / (1024 ** 2))
else:
return '%iG' % (size / (1024 ** 3)) | Format the given file size in bytes to human readable format. | formatFileSize | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _getFilesAndDirectories(self, directory):
"""
Helper returning files and directories in given directory listing, with
attributes to be used to build a table content with
C{self.linePattern}.
@return: tuple of (directories, files)
@rtype: C{tuple} of C{list}
"""
files = []
dirs = []
for path in directory:
if _PY3:
if isinstance(path, bytes):
path = path.decode("utf8")
url = quote(path, "/")
escapedPath = escape(path)
childPath = filepath.FilePath(self.path).child(path)
if childPath.isdir():
dirs.append({'text': escapedPath + "/", 'href': url + "/",
'size': '', 'type': '[Directory]',
'encoding': ''})
else:
mimetype, encoding = getTypeAndEncoding(path, self.contentTypes,
self.contentEncodings,
self.defaultType)
try:
size = childPath.getsize()
except OSError:
continue
files.append({
'text': escapedPath, "href": url,
'type': '[%s]' % mimetype,
'encoding': (encoding and '[%s]' % encoding or ''),
'size': formatFileSize(size)})
return dirs, files | Helper returning files and directories in given directory listing, with
attributes to be used to build a table content with
C{self.linePattern}.
@return: tuple of (directories, files)
@rtype: C{tuple} of C{list} | _getFilesAndDirectories | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def _buildTableContent(self, elements):
"""
Build a table content using C{self.linePattern} and giving elements odd
and even classes.
"""
tableContent = []
rowClasses = itertools.cycle(['odd', 'even'])
for element, rowClass in zip(elements, rowClasses):
element["class"] = rowClass
tableContent.append(self.linePattern % element)
return tableContent | Build a table content using C{self.linePattern} and giving elements odd
and even classes. | _buildTableContent | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def render(self, request):
"""
Render a listing of the content of C{self.path}.
"""
request.setHeader(b"content-type", b"text/html; charset=utf-8")
if self.dirs is None:
directory = os.listdir(self.path)
directory.sort()
else:
directory = self.dirs
dirs, files = self._getFilesAndDirectories(directory)
tableContent = "".join(self._buildTableContent(dirs + files))
header = "Directory listing for %s" % (
escape(unquote(nativeString(request.uri))),)
done = self.template % {"header": header, "tableContent": tableContent}
if _PY3:
done = done.encode("utf8")
return done | Render a listing of the content of C{self.path}. | render | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/static.py | MIT |
def connectionMade(self):
"""
Called by the reactor when a connection is received. May also be called
by the L{twisted.web.http._GenericHTTPChannelProtocol} during upgrade
to HTTP/2.
"""
self.setTimeout(self.timeOut)
self.conn.initiate_connection()
self.transport.write(self.conn.data_to_send()) | Called by the reactor when a connection is received. May also be called
by the L{twisted.web.http._GenericHTTPChannelProtocol} during upgrade
to HTTP/2. | connectionMade | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def dataReceived(self, data):
"""
Called whenever a chunk of data is received from the transport.
@param data: The data received from the transport.
@type data: L{bytes}
"""
self.resetTimeout()
try:
events = self.conn.receive_data(data)
except h2.exceptions.ProtocolError:
# A remote protocol error terminates the connection.
dataToSend = self.conn.data_to_send()
self.transport.write(dataToSend)
self.transport.loseConnection()
self.connectionLost(Failure())
return
for event in events:
if isinstance(event, h2.events.RequestReceived):
self._requestReceived(event)
elif isinstance(event, h2.events.DataReceived):
self._requestDataReceived(event)
elif isinstance(event, h2.events.StreamEnded):
self._requestEnded(event)
elif isinstance(event, h2.events.StreamReset):
self._requestAborted(event)
elif isinstance(event, h2.events.WindowUpdated):
self._handleWindowUpdate(event)
elif isinstance(event, h2.events.PriorityUpdated):
self._handlePriorityUpdate(event)
elif isinstance(event, h2.events.ConnectionTerminated):
self.transport.loseConnection()
self.connectionLost(ConnectionLost("Remote peer sent GOAWAY"))
dataToSend = self.conn.data_to_send()
if dataToSend:
self.transport.write(dataToSend) | Called whenever a chunk of data is received from the transport.
@param data: The data received from the transport.
@type data: L{bytes} | dataReceived | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def timeoutConnection(self):
"""
Called when the connection has been inactive for
L{self.timeOut<twisted.protocols.policies.TimeoutMixin.timeOut>}
seconds. Cleanly tears the connection down, attempting to notify the
peer if needed.
We override this method to add two extra bits of functionality:
- We want to log the timeout.
- We want to send a GOAWAY frame indicating that the connection is
being terminated, and whether it was clean or not. We have to do this
before the connection is torn down.
"""
self._log.info(
"Timing out client {client}", client=self.transport.getPeer()
)
# Check whether there are open streams. If there are, we're going to
# want to use the error code PROTOCOL_ERROR. If there aren't, use
# NO_ERROR.
if (self.conn.open_outbound_streams > 0 or
self.conn.open_inbound_streams > 0):
error_code = h2.errors.ErrorCodes.PROTOCOL_ERROR
else:
error_code = h2.errors.ErrorCodes.NO_ERROR
self.conn.close_connection(error_code=error_code)
self.transport.write(self.conn.data_to_send())
# Don't let the client hold this connection open too long.
if self.abortTimeout is not None:
# We use self.callLater because that's what TimeoutMixin does, even
# though we have a perfectly good reactor sitting around. See
# https://twistedmatrix.com/trac/ticket/8488.
self._abortingCall = self.callLater(
self.abortTimeout, self.forceAbortClient
)
# We're done, throw the connection away.
self.transport.loseConnection() | Called when the connection has been inactive for
L{self.timeOut<twisted.protocols.policies.TimeoutMixin.timeOut>}
seconds. Cleanly tears the connection down, attempting to notify the
peer if needed.
We override this method to add two extra bits of functionality:
- We want to log the timeout.
- We want to send a GOAWAY frame indicating that the connection is
being terminated, and whether it was clean or not. We have to do this
before the connection is torn down. | timeoutConnection | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def forceAbortClient(self):
"""
Called if C{abortTimeout} seconds have passed since the timeout fired,
and the connection still hasn't gone away. This can really only happen
on extremely bad connections or when clients are maliciously attempting
to keep connections open.
"""
self._log.info(
"Forcibly timing out client: {client}",
client=self.transport.getPeer()
)
# We want to lose track of the _abortingCall so that no-one tries to
# cancel it.
self._abortingCall = None
self.transport.abortConnection() | Called if C{abortTimeout} seconds have passed since the timeout fired,
and the connection still hasn't gone away. This can really only happen
on extremely bad connections or when clients are maliciously attempting
to keep connections open. | forceAbortClient | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def connectionLost(self, reason):
"""
Called when the transport connection is lost.
Informs all outstanding response handlers that the connection has been
lost, and cleans up all internal state.
"""
self._stillProducing = False
self.setTimeout(None)
for stream in self.streams.values():
stream.connectionLost(reason)
for streamID in list(self.streams.keys()):
self._requestDone(streamID)
# If we were going to force-close the transport, we don't have to now.
if self._abortingCall is not None:
self._abortingCall.cancel()
self._abortingCall = None | Called when the transport connection is lost.
Informs all outstanding response handlers that the connection has been
lost, and cleans up all internal state. | connectionLost | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def stopProducing(self):
"""
Stop producing data.
This tells the L{H2Connection} that its consumer has died, so it must
stop producing data for good.
"""
self.connectionLost(ConnectionLost("Producing stopped")) | Stop producing data.
This tells the L{H2Connection} that its consumer has died, so it must
stop producing data for good. | stopProducing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def pauseProducing(self):
"""
Pause producing data.
Tells the L{H2Connection} that it has produced too much data to process
for the time being, and to stop until resumeProducing() is called.
"""
self._consumerBlocked = Deferred() | Pause producing data.
Tells the L{H2Connection} that it has produced too much data to process
for the time being, and to stop until resumeProducing() is called. | pauseProducing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def resumeProducing(self):
"""
Resume producing data.
This tells the L{H2Connection} to re-add itself to the main loop and
produce more data for the consumer.
"""
if self._consumerBlocked is not None:
d = self._consumerBlocked
self._consumerBlocked = None
d.callback(None) | Resume producing data.
This tells the L{H2Connection} to re-add itself to the main loop and
produce more data for the consumer. | resumeProducing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _sendPrioritisedData(self, *args):
"""
The data sending loop. This function repeatedly calls itself, either
from L{Deferred}s or from
L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
This function sends data on streams according to the rules of HTTP/2
priority. It ensures that the data from each stream is interleved
according to the priority signalled by the client, making sure that the
connection is used with maximal efficiency.
This function will execute if data is available: if all data is
exhausted, the function will place a deferred onto the L{H2Connection}
object and wait until it is called to resume executing.
"""
# If producing has stopped, we're done. Don't reschedule ourselves
if not self._stillProducing:
return
stream = None
while stream is None:
try:
stream = next(self.priority)
except priority.DeadlockError:
# All streams are currently blocked or not progressing. Wait
# until a new one becomes available.
assert self._sendingDeferred is None
self._sendingDeferred = Deferred()
self._sendingDeferred.addCallback(self._sendPrioritisedData)
return
# Wait behind the transport.
if self._consumerBlocked is not None:
self._consumerBlocked.addCallback(self._sendPrioritisedData)
return
self.resetTimeout()
remainingWindow = self.conn.local_flow_control_window(stream)
frameData = self._outboundStreamQueues[stream].popleft()
maxFrameSize = min(self.conn.max_outbound_frame_size, remainingWindow)
if frameData is _END_STREAM_SENTINEL:
# There's no error handling here even though this can throw
# ProtocolError because we really shouldn't encounter this problem.
# If we do, that's a nasty bug.
self.conn.end_stream(stream)
self.transport.write(self.conn.data_to_send())
# Clean up the stream
self._requestDone(stream)
else:
# Respect the max frame size.
if len(frameData) > maxFrameSize:
excessData = frameData[maxFrameSize:]
frameData = frameData[:maxFrameSize]
self._outboundStreamQueues[stream].appendleft(excessData)
# There's deliberately no error handling here, because this just
# absolutely should not happen.
# If for whatever reason the max frame length is zero and so we
# have no frame data to send, don't send any.
if frameData:
self.conn.send_data(stream, frameData)
self.transport.write(self.conn.data_to_send())
# If there's no data left, this stream is now blocked.
if not self._outboundStreamQueues[stream]:
self.priority.block(stream)
# Also, if the stream's flow control window is exhausted, tell it
# to stop.
if self.remainingOutboundWindow(stream) <= 0:
self.streams[stream].flowControlBlocked()
self._reactor.callLater(0, self._sendPrioritisedData) | The data sending loop. This function repeatedly calls itself, either
from L{Deferred}s or from
L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
This function sends data on streams according to the rules of HTTP/2
priority. It ensures that the data from each stream is interleved
according to the priority signalled by the client, making sure that the
connection is used with maximal efficiency.
This function will execute if data is available: if all data is
exhausted, the function will place a deferred onto the L{H2Connection}
object and wait until it is called to resume executing. | _sendPrioritisedData | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _requestReceived(self, event):
"""
Internal handler for when a request has been received.
@param event: The Hyper-h2 event that encodes information about the
received request.
@type event: L{h2.events.RequestReceived}
"""
stream = H2Stream(
event.stream_id,
self, event.headers,
self.requestFactory,
self.site,
self.factory
)
self.streams[event.stream_id] = stream
self._streamCleanupCallbacks[event.stream_id] = Deferred()
self._outboundStreamQueues[event.stream_id] = deque()
# Add the stream to the priority tree but immediately block it.
try:
self.priority.insert_stream(event.stream_id)
except priority.DuplicateStreamError:
# Stream already in the tree. This can happen if we received a
# PRIORITY frame before a HEADERS frame. Just move on: we set the
# stream up properly in _handlePriorityUpdate.
pass
else:
self.priority.block(event.stream_id) | Internal handler for when a request has been received.
@param event: The Hyper-h2 event that encodes information about the
received request.
@type event: L{h2.events.RequestReceived} | _requestReceived | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _requestDataReceived(self, event):
"""
Internal handler for when a chunk of data is received for a given
request.
@param event: The Hyper-h2 event that encodes information about the
received data.
@type event: L{h2.events.DataReceived}
"""
stream = self.streams[event.stream_id]
stream.receiveDataChunk(event.data, event.flow_controlled_length) | Internal handler for when a chunk of data is received for a given
request.
@param event: The Hyper-h2 event that encodes information about the
received data.
@type event: L{h2.events.DataReceived} | _requestDataReceived | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _requestEnded(self, event):
"""
Internal handler for when a request is complete, and we expect no
further data for that request.
@param event: The Hyper-h2 event that encodes information about the
completed stream.
@type event: L{h2.events.StreamEnded}
"""
stream = self.streams[event.stream_id]
stream.requestComplete() | Internal handler for when a request is complete, and we expect no
further data for that request.
@param event: The Hyper-h2 event that encodes information about the
completed stream.
@type event: L{h2.events.StreamEnded} | _requestEnded | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _requestAborted(self, event):
"""
Internal handler for when a request is aborted by a remote peer.
@param event: The Hyper-h2 event that encodes information about the
reset stream.
@type event: L{h2.events.StreamReset}
"""
stream = self.streams[event.stream_id]
stream.connectionLost(
ConnectionLost("Stream reset with code %s" % event.error_code)
)
self._requestDone(event.stream_id) | Internal handler for when a request is aborted by a remote peer.
@param event: The Hyper-h2 event that encodes information about the
reset stream.
@type event: L{h2.events.StreamReset} | _requestAborted | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _handlePriorityUpdate(self, event):
"""
Internal handler for when a stream priority is updated.
@param event: The Hyper-h2 event that encodes information about the
stream reprioritization.
@type event: L{h2.events.PriorityUpdated}
"""
try:
self.priority.reprioritize(
stream_id=event.stream_id,
depends_on=event.depends_on or None,
weight=event.weight,
exclusive=event.exclusive,
)
except priority.MissingStreamError:
# A PRIORITY frame arrived before the HEADERS frame that would
# trigger us to insert the stream into the tree. That's fine: we
# can create the stream here and mark it as blocked.
self.priority.insert_stream(
stream_id=event.stream_id,
depends_on=event.depends_on or None,
weight=event.weight,
exclusive=event.exclusive,
)
self.priority.block(event.stream_id) | Internal handler for when a stream priority is updated.
@param event: The Hyper-h2 event that encodes information about the
stream reprioritization.
@type event: L{h2.events.PriorityUpdated} | _handlePriorityUpdate | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def writeHeaders(self, version, code, reason, headers, streamID):
"""
Called by L{twisted.web.http.Request} objects to write a complete set
of HTTP headers to a stream.
@param version: The HTTP version in use. Unused in HTTP/2.
@type version: L{bytes}
@param code: The HTTP status code to write.
@type code: L{bytes}
@param reason: The HTTP reason phrase to write. Unused in HTTP/2.
@type reason: L{bytes}
@param headers: The headers to write to the stream.
@type headers: L{twisted.web.http_headers.Headers}
@param streamID: The ID of the stream to write the headers to.
@type streamID: L{int}
"""
headers.insert(0, (b':status', code))
try:
self.conn.send_headers(streamID, headers)
except h2.exceptions.StreamClosedError:
# Stream was closed by the client at some point. We need to not
# explode here: just swallow the error. That's what write() does
# when a connection is lost, so that's what we do too.
return
else:
self.transport.write(self.conn.data_to_send()) | Called by L{twisted.web.http.Request} objects to write a complete set
of HTTP headers to a stream.
@param version: The HTTP version in use. Unused in HTTP/2.
@type version: L{bytes}
@param code: The HTTP status code to write.
@type code: L{bytes}
@param reason: The HTTP reason phrase to write. Unused in HTTP/2.
@type reason: L{bytes}
@param headers: The headers to write to the stream.
@type headers: L{twisted.web.http_headers.Headers}
@param streamID: The ID of the stream to write the headers to.
@type streamID: L{int} | writeHeaders | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def writeDataToStream(self, streamID, data):
"""
May be called by L{H2Stream} objects to write response data to a given
stream. Writes a single data frame.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int}
@param data: The data chunk to write to the stream.
@type data: L{bytes}
"""
self._outboundStreamQueues[streamID].append(data)
# There's obviously no point unblocking this stream and the sending
# loop if the data can't actually be sent, so confirm that there's
# some room to send data.
if self.conn.local_flow_control_window(streamID) > 0:
self.priority.unblock(streamID)
if self._sendingDeferred is not None:
d = self._sendingDeferred
self._sendingDeferred = None
d.callback(streamID)
if self.remainingOutboundWindow(streamID) <= 0:
self.streams[streamID].flowControlBlocked() | May be called by L{H2Stream} objects to write response data to a given
stream. Writes a single data frame.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int}
@param data: The data chunk to write to the stream.
@type data: L{bytes} | writeDataToStream | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def endRequest(self, streamID):
"""
Called by L{H2Stream} objects to signal completion of a response.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int}
"""
self._outboundStreamQueues[streamID].append(_END_STREAM_SENTINEL)
self.priority.unblock(streamID)
if self._sendingDeferred is not None:
d = self._sendingDeferred
self._sendingDeferred = None
d.callback(streamID) | Called by L{H2Stream} objects to signal completion of a response.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int} | endRequest | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def abortRequest(self, streamID):
"""
Called by L{H2Stream} objects to request early termination of a stream.
This emits a RstStream frame and then removes all stream state.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int}
"""
self.conn.reset_stream(streamID)
self.transport.write(self.conn.data_to_send())
self._requestDone(streamID) | Called by L{H2Stream} objects to request early termination of a stream.
This emits a RstStream frame and then removes all stream state.
@param streamID: The ID of the stream to write the data to.
@type streamID: L{int} | abortRequest | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _requestDone(self, streamID):
"""
Called internally by the data sending loop to clean up state that was
being used for the stream. Called when the stream is complete.
@param streamID: The ID of the stream to clean up state for.
@type streamID: L{int}
"""
del self._outboundStreamQueues[streamID]
self.priority.remove_stream(streamID)
del self.streams[streamID]
cleanupCallback = self._streamCleanupCallbacks.pop(streamID)
cleanupCallback.callback(streamID) | Called internally by the data sending loop to clean up state that was
being used for the stream. Called when the stream is complete.
@param streamID: The ID of the stream to clean up state for.
@type streamID: L{int} | _requestDone | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def remainingOutboundWindow(self, streamID):
"""
Called to determine how much room is left in the send window for a
given stream. Allows us to handle blocking and unblocking producers.
@param streamID: The ID of the stream whose flow control window we'll
check.
@type streamID: L{int}
@return: The amount of room remaining in the send window for the given
stream, including the data queued to be sent.
@rtype: L{int}
"""
# TODO: This involves a fair bit of looping and computation for
# something that is called a lot. Consider caching values somewhere.
windowSize = self.conn.local_flow_control_window(streamID)
sendQueue = self._outboundStreamQueues[streamID]
alreadyConsumed = sum(
len(chunk) for chunk in sendQueue
if chunk is not _END_STREAM_SENTINEL
)
return windowSize - alreadyConsumed | Called to determine how much room is left in the send window for a
given stream. Allows us to handle blocking and unblocking producers.
@param streamID: The ID of the stream whose flow control window we'll
check.
@type streamID: L{int}
@return: The amount of room remaining in the send window for the given
stream, including the data queued to be sent.
@rtype: L{int} | remainingOutboundWindow | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _handleWindowUpdate(self, event):
"""
Manage flow control windows.
Streams that are blocked on flow control will register themselves with
the connection. This will fire deferreds that wake those streams up and
allow them to continue processing.
@param event: The Hyper-h2 event that encodes information about the
flow control window change.
@type event: L{h2.events.WindowUpdated}
"""
streamID = event.stream_id
if streamID:
if not self._streamIsActive(streamID):
# We may have already cleaned up our stream state, making this
# a late WINDOW_UPDATE frame. That's fine: the update is
# unnecessary but benign. We'll ignore it.
return
# If we haven't got any data to send, don't unblock the stream. If
# we do, we'll eventually get an exception inside the
# _sendPrioritisedData loop some time later.
if self._outboundStreamQueues.get(streamID):
self.priority.unblock(streamID)
self.streams[streamID].windowUpdated()
else:
# Update strictly applies to all streams.
for stream in self.streams.values():
stream.windowUpdated()
# If we still have data to send for this stream, unblock it.
if self._outboundStreamQueues.get(stream.streamID):
self.priority.unblock(stream.streamID) | Manage flow control windows.
Streams that are blocked on flow control will register themselves with
the connection. This will fire deferreds that wake those streams up and
allow them to continue processing.
@param event: The Hyper-h2 event that encodes information about the
flow control window change.
@type event: L{h2.events.WindowUpdated} | _handleWindowUpdate | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def openStreamWindow(self, streamID, increment):
"""
Open the stream window by a given increment.
@param streamID: The ID of the stream whose window needs to be opened.
@type streamID: L{int}
@param increment: The amount by which the stream window must be
incremented.
@type increment: L{int}
"""
self.conn.acknowledge_received_data(increment, streamID)
data = self.conn.data_to_send()
if data:
self.transport.write(data) | Open the stream window by a given increment.
@param streamID: The ID of the stream whose window needs to be opened.
@type streamID: L{int}
@param increment: The amount by which the stream window must be
incremented.
@type increment: L{int} | openStreamWindow | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _isSecure(self):
"""
Returns L{True} if this channel is using a secure transport.
@returns: L{True} if this channel is secure.
@rtype: L{bool}
"""
# A channel is secure if its transport is ISSLTransport.
return ISSLTransport(self.transport, None) is not None | Returns L{True} if this channel is using a secure transport.
@returns: L{True} if this channel is secure.
@rtype: L{bool} | _isSecure | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _send100Continue(self, streamID):
"""
Sends a 100 Continue response, used to signal to clients that further
processing will be performed.
@param streamID: The ID of the stream that needs the 100 Continue
response
@type streamID: L{int}
"""
headers = [(b':status', b'100')]
self.conn.send_headers(headers=headers, stream_id=streamID)
self.transport.write(self.conn.data_to_send()) | Sends a 100 Continue response, used to signal to clients that further
processing will be performed.
@param streamID: The ID of the stream that needs the 100 Continue
response
@type streamID: L{int} | _send100Continue | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _respondToBadRequestAndDisconnect(self, streamID):
"""
This is a quick and dirty way of responding to bad requests.
As described by HTTP standard we should be patient and accept the
whole request from the client before sending a polite bad request
response, even in the case when clients send tons of data.
Unlike in the HTTP/1.1 case, this does not actually disconnect the
underlying transport: there's no need. This instead just sends a 400
response and terminates the stream.
@param streamID: The ID of the stream that needs the 100 Continue
response
@type streamID: L{int}
"""
headers = [(b':status', b'400')]
self.conn.send_headers(
headers=headers,
stream_id=streamID,
end_stream=True
)
self.transport.write(self.conn.data_to_send())
stream = self.streams[streamID]
stream.connectionLost(ConnectionLost("Invalid request"))
self._requestDone(streamID) | This is a quick and dirty way of responding to bad requests.
As described by HTTP standard we should be patient and accept the
whole request from the client before sending a polite bad request
response, even in the case when clients send tons of data.
Unlike in the HTTP/1.1 case, this does not actually disconnect the
underlying transport: there's no need. This instead just sends a 400
response and terminates the stream.
@param streamID: The ID of the stream that needs the 100 Continue
response
@type streamID: L{int} | _respondToBadRequestAndDisconnect | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _streamIsActive(self, streamID):
"""
Checks whether Twisted has still got state for a given stream and so
can process events for that stream.
@param streamID: The ID of the stream that needs processing.
@type streamID: L{int}
@return: Whether the stream still has state allocated.
@rtype: L{bool}
"""
return streamID in self.streams | Checks whether Twisted has still got state for a given stream and so
can process events for that stream.
@param streamID: The ID of the stream that needs processing.
@type streamID: L{int}
@return: Whether the stream still has state allocated.
@rtype: L{bool} | _streamIsActive | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def __init__(self, streamID, connection, headers,
requestFactory, site, factory):
"""
Initialize this HTTP/2 stream.
@param streamID: The numerical stream ID that this object corresponds
to.
@type streamID: L{int}
@param connection: The HTTP/2 connection this stream belongs to.
@type connection: L{H2Connection}
@param headers: The HTTP/2 request headers.
@type headers: A L{list} of L{tuple}s of header name and header value,
both as L{bytes}.
@param requestFactory: A function that builds appropriate request
request objects.
@type requestFactory: A callable that returns a
L{twisted.web.iweb.IRequest}.
@param site: The L{twisted.web.server.Site} object this stream belongs
to, if any.
@type site: L{twisted.web.server.Site}
@param factory: The L{twisted.web.http.HTTPFactory} object that
constructed this stream's parent connection.
@type factory: L{twisted.web.http.HTTPFactory}
"""
self.streamID = streamID
self.site = site
self.factory = factory
self.producing = True
self.command = None
self.path = None
self.producer = None
self._producerProducing = False
self._hasStreamingProducer = None
self._inboundDataBuffer = deque()
self._conn = connection
self._request = requestFactory(self, queued=False)
self._buffer = io.BytesIO()
self._convertHeaders(headers) | Initialize this HTTP/2 stream.
@param streamID: The numerical stream ID that this object corresponds
to.
@type streamID: L{int}
@param connection: The HTTP/2 connection this stream belongs to.
@type connection: L{H2Connection}
@param headers: The HTTP/2 request headers.
@type headers: A L{list} of L{tuple}s of header name and header value,
both as L{bytes}.
@param requestFactory: A function that builds appropriate request
request objects.
@type requestFactory: A callable that returns a
L{twisted.web.iweb.IRequest}.
@param site: The L{twisted.web.server.Site} object this stream belongs
to, if any.
@type site: L{twisted.web.server.Site}
@param factory: The L{twisted.web.http.HTTPFactory} object that
constructed this stream's parent connection.
@type factory: L{twisted.web.http.HTTPFactory} | __init__ | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def _convertHeaders(self, headers):
"""
This method converts the HTTP/2 header set into something that looks
like HTTP/1.1. In particular, it strips the 'special' headers and adds
a Host: header.
@param headers: The HTTP/2 header set.
@type headers: A L{list} of L{tuple}s of header name and header value,
both as L{bytes}.
"""
gotLength = False
for header in headers:
if not header[0].startswith(b':'):
gotLength = (
_addHeaderToRequest(self._request, header) or gotLength
)
elif header[0] == b':method':
self.command = header[1]
elif header[0] == b':path':
self.path = header[1]
elif header[0] == b':authority':
# This is essentially the Host: header from HTTP/1.1
_addHeaderToRequest(self._request, (b'host', header[1]))
if not gotLength:
if self.command in (b'GET', b'HEAD'):
self._request.gotLength(0)
else:
self._request.gotLength(None)
self._request.parseCookies()
expectContinue = self._request.requestHeaders.getRawHeaders(b'expect')
if expectContinue and expectContinue[0].lower() == b'100-continue':
self._send100Continue() | This method converts the HTTP/2 header set into something that looks
like HTTP/1.1. In particular, it strips the 'special' headers and adds
a Host: header.
@param headers: The HTTP/2 header set.
@type headers: A L{list} of L{tuple}s of header name and header value,
both as L{bytes}. | _convertHeaders | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def receiveDataChunk(self, data, flowControlledLength):
"""
Called when the connection has received a chunk of data from the
underlying transport. If the stream has been registered with a
consumer, and is currently able to push data, immediately passes it
through. Otherwise, buffers the chunk until we can start producing.
@param data: The chunk of data that was received.
@type data: L{bytes}
@param flowControlledLength: The total flow controlled length of this
chunk, which is used when we want to re-open the window. May be
different to C{len(data)}.
@type flowControlledLength: L{int}
"""
if not self.producing:
# Buffer data.
self._inboundDataBuffer.append((data, flowControlledLength))
else:
self._request.handleContentChunk(data)
self._conn.openStreamWindow(self.streamID, flowControlledLength) | Called when the connection has received a chunk of data from the
underlying transport. If the stream has been registered with a
consumer, and is currently able to push data, immediately passes it
through. Otherwise, buffers the chunk until we can start producing.
@param data: The chunk of data that was received.
@type data: L{bytes}
@param flowControlledLength: The total flow controlled length of this
chunk, which is used when we want to re-open the window. May be
different to C{len(data)}.
@type flowControlledLength: L{int} | receiveDataChunk | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def requestComplete(self):
"""
Called by the L{H2Connection} when the all data for a request has been
received. Currently, with the legacy L{twisted.web.http.Request}
object, just calls requestReceived unless the producer wants us to be
quiet.
"""
if self.producing:
self._request.requestReceived(self.command, self.path, b'HTTP/2')
else:
self._inboundDataBuffer.append((_END_STREAM_SENTINEL, None)) | Called by the L{H2Connection} when the all data for a request has been
received. Currently, with the legacy L{twisted.web.http.Request}
object, just calls requestReceived unless the producer wants us to be
quiet. | requestComplete | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def connectionLost(self, reason):
"""
Called by the L{H2Connection} when a connection is lost or a stream is
reset.
@param reason: The reason the connection was lost.
@type reason: L{str}
"""
self._request.connectionLost(reason) | Called by the L{H2Connection} when a connection is lost or a stream is
reset.
@param reason: The reason the connection was lost.
@type reason: L{str} | connectionLost | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def windowUpdated(self):
"""
Called by the L{H2Connection} when this stream's flow control window
has been opened.
"""
# If we don't have a producer, we have no-one to tell.
if not self.producer:
return
# If we're not blocked on flow control, we don't care.
if self._producerProducing:
return
# We check whether the stream's flow control window is actually above
# 0, and then, if a producer is registered and we still have space in
# the window, we unblock it.
remainingWindow = self._conn.remainingOutboundWindow(self.streamID)
if not remainingWindow > 0:
return
# We have a producer and space in the window, so that producer can
# start producing again!
self._producerProducing = True
self.producer.resumeProducing() | Called by the L{H2Connection} when this stream's flow control window
has been opened. | windowUpdated | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def flowControlBlocked(self):
"""
Called by the L{H2Connection} when this stream's flow control window
has been exhausted.
"""
if not self.producer:
return
if self._producerProducing:
self.producer.pauseProducing()
self._producerProducing = False | Called by the L{H2Connection} when this stream's flow control window
has been exhausted. | flowControlBlocked | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def writeHeaders(self, version, code, reason, headers):
"""
Called by the consumer to write headers to the stream.
@param version: The HTTP version.
@type version: L{bytes}
@param code: The status code.
@type code: L{int}
@param reason: The reason phrase. Ignored in HTTP/2.
@type reason: L{bytes}
@param headers: The HTTP response headers.
@type: Any iterable of two-tuples of L{bytes}, representing header
names and header values.
"""
self._conn.writeHeaders(version, code, reason, headers, self.streamID) | Called by the consumer to write headers to the stream.
@param version: The HTTP version.
@type version: L{bytes}
@param code: The status code.
@type code: L{int}
@param reason: The reason phrase. Ignored in HTTP/2.
@type reason: L{bytes}
@param headers: The HTTP response headers.
@type: Any iterable of two-tuples of L{bytes}, representing header
names and header values. | writeHeaders | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
def requestDone(self, request):
"""
Called by a consumer to clean up whatever permanent state is in use.
@param request: The request calling the method.
@type request: L{twisted.web.iweb.IRequest}
"""
self._conn.endRequest(self.streamID) | Called by a consumer to clean up whatever permanent state is in use.
@param request: The request calling the method.
@type request: L{twisted.web.iweb.IRequest} | requestDone | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/web/_http2.py | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.