-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostBigExtractRequest.php
164 lines (145 loc) · 5.56 KB
/
PostBigExtractRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* PostBigExtractRequest
*
* Version: 0.2
* Author: Andrea Di Pietro
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace Solarium\Plugin;
use Solarium\Core\Client\Adapter\AdapterHelper;
use Solarium\Core\Client\Request;
use Solarium\Core\Event\Events;
use Solarium\Core\Event\PostCreateRequest as PostCreateRequestEvent;
use Solarium\Core\Plugin\AbstractPlugin;
/**
* PostBigExtractRequest plugin.
*
* If you reach the url/header length limit of your servlet container your queries will fail.
* You can increase the limit in the servlet container, but if that's not possible this plugin can automatically
* convert big literals query string into multipart POST parameters. POST parameters (usually) has a much higher limit.
*
* The default maximum querystring length is 1024. This doesn't include the base url or headers.
* For most servlet setups this limit leaves enough room for that extra data. Adjust the limit if needed.
*/
class PostBigExtractRequest extends AbstractPlugin
{
/**
* Default options.
*
* @var array
*/
protected $options = [
'maxquerystringlength' => 1024,
'charset' => 'UTF-8',
];
/**
* Set maxquerystringlength enabled option.
*
* @param int $value
*
* @return self Provides fluent interface
*/
public function setMaxQueryStringLength(int $value): self
{
$this->setOption('maxquerystringlength', $value);
return $this;
}
/**
* Get maxquerystringlength option.
*
* @return int|null
*/
public function getMaxQueryStringLength(): ?int
{
return $this->getOption('maxquerystringlength');
}
/**
* Set charset enabled option.
*
* @param string $value
*
* @return self Provides fluent interface
*/
public function setCharset(string $value): self
{
$this->setOption('charset', $value);
return $this;
}
/**
* Get charset option.
*
* @return string|null
*/
public function getCharset(): ?string
{
return $this->getOption('charset');
}
/**
* Event hook to adjust client settings just before query execution.
*
* @param PostCreateRequestEvent $event
*
* @return self Provides fluent interface
*/
public function postCreateRequest(PostCreateRequestEvent $event): self
{
$request = $event->getRequest();
$queryString = $request->getQueryString();
if ('update/extract'==$request->getHandler() && strlen($queryString) > $this->getMaxQueryStringLength()) {
if ($request->getFileUpload()) {
$body = '';
$params = $request->getParams();
if( !empty($params) && count($params)>0 ):
foreach($params as $key=>$value):
if( is_countable($value) ):
foreach ( $value as $array_key => $array_val ):
$additional_body_header;
if( is_string ( $array_val ) /*&& 1!== preg_match('/.*[^a-zA-Z0-9]{1}bin$/', $key)*/ ):
$additional_body_header = "\r\nContent-Type: text/plain;charset=" . $this->getCharset();//$value = urlencode($value);
else:
$additional_body_header = '';
endif;
$body .= "--{$request->getHash()}\r\n";
$body .= 'Content-Disposition: form-data; name="' . $key . '"';
$body .= $additional_body_header;
$body .= "\r\n\r\n";
$body .= $array_val;
$body .= "\r\n";
endforeach;
else:
$additional_body_header;
if( is_string ( $value ) /*&& 1!== preg_match('/.*[^a-zA-Z0-9]{1}bin$/', $key)*/ ):
$additional_body_header = "\r\nContent-Type: text/plain;charset=" . $this->getCharset();//$value = urlencode($value);
else:
$additional_body_header = '';
endif;
$body .= "--{$request->getHash()}\r\n";
$body .= 'Content-Disposition: form-data; name="' . $key . '"';
$body .= $additional_body_header;
$body .= "\r\n\r\n";
$body .= $value;
$body .= "\r\n";
endif;
endforeach;
endif;
$body .= AdapterHelper::buildUploadBodyFromRequest( $request ); //must be the last automatically include closing boundary
$request->setRawData( $body );
$request->setOption('file', null); // this prevent solarium from call AdapterHelper::buildUploadBodyFromRequest for setting body request
$request->clearParams();
}
}
return $this;
}
/**
* Plugin init function.
*
* Register event listeners
*/
protected function initPluginType()
{
$dispatcher = $this->client->getEventDispatcher();
$dispatcher->addListener(Events::POST_CREATE_REQUEST, [$this, 'postCreateRequest']);
}
}