comparison OrthancServer/LuaScripting.cpp @ 3442:dd1e68f2d0c0

Fix issue #106 (Unable to export preview as jpeg from Lua script)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 24 Jun 2019 16:06:47 +0200
parents 4bbadcd03966
children 94f4a18a79cc
comparison
equal deleted inserted replaced
3441:6cc72ebfd6ef 3442:dd1e68f2d0c0
277 return 1; 277 return 1;
278 } 278 }
279 279
280 // Check the types of the arguments 280 // Check the types of the arguments
281 int nArgs = lua_gettop(state); 281 int nArgs = lua_gettop(state);
282 if ((nArgs != 1 && nArgs != 2) || 282 if (nArgs < 1 || nArgs > 3 ||
283 !lua_isstring(state, 1) || // URI 283 !lua_isstring(state, 1) || // URI
284 (nArgs == 2 && !lua_isboolean(state, 2))) // Restrict to built-in API? 284 (nArgs >= 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
285 { 285 {
286 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()"; 286 LOG(ERROR) << "Lua: Bad parameters to RestApiGet()";
287 lua_pushnil(state); 287 lua_pushnil(state);
288 return 1; 288 return 1;
289 } 289 }
290 290
291 const char* uri = lua_tostring(state, 1); 291 const char* uri = lua_tostring(state, 1);
292 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false); 292 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
293 293
294 std::map<std::string, std::string> headers;
295 LuaContext::GetDictionaryArgument(headers, state, 3, true /* HTTP header key to lower case */);
296
294 try 297 try
295 { 298 {
296 std::string result; 299 std::string result;
297 if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 300 if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
298 RequestOrigin_Lua, uri)) 301 RequestOrigin_Lua, uri, headers))
299 { 302 {
300 lua_pushlstring(state, result.c_str(), result.size()); 303 lua_pushlstring(state, result.c_str(), result.size());
301 return 1; 304 return 1;
302 } 305 }
303 } 306 }
323 return 1; 326 return 1;
324 } 327 }
325 328
326 // Check the types of the arguments 329 // Check the types of the arguments
327 int nArgs = lua_gettop(state); 330 int nArgs = lua_gettop(state);
328 if ((nArgs != 2 && nArgs != 3) || 331 if (nArgs < 2 || nArgs > 4 ||
329 !lua_isstring(state, 1) || // URI 332 !lua_isstring(state, 1) || // URI
330 !lua_isstring(state, 2) || // Body 333 !lua_isstring(state, 2) || // Body
331 (nArgs == 3 && !lua_isboolean(state, 3))) // Restrict to built-in API? 334 (nArgs >= 3 && !lua_isboolean(state, 3))) // Restrict to built-in API?
332 { 335 {
333 LOG(ERROR) << "Lua: Bad parameters to " << (isPost ? "RestApiPost()" : "RestApiPut()"); 336 LOG(ERROR) << "Lua: Bad parameters to " << (isPost ? "RestApiPost()" : "RestApiPut()");
334 lua_pushnil(state); 337 lua_pushnil(state);
335 return 1; 338 return 1;
336 } 339 }
338 const char* uri = lua_tostring(state, 1); 341 const char* uri = lua_tostring(state, 1);
339 size_t bodySize = 0; 342 size_t bodySize = 0;
340 const char* bodyData = lua_tolstring(state, 2, &bodySize); 343 const char* bodyData = lua_tolstring(state, 2, &bodySize);
341 bool builtin = (nArgs == 3 ? lua_toboolean(state, 3) != 0 : false); 344 bool builtin = (nArgs == 3 ? lua_toboolean(state, 3) != 0 : false);
342 345
346 std::map<std::string, std::string> headers;
347 LuaContext::GetDictionaryArgument(headers, state, 4, true /* HTTP header key to lower case */);
348
343 try 349 try
344 { 350 {
345 std::string result; 351 std::string result;
346 if (isPost ? 352 if (isPost ?
347 HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 353 HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
348 RequestOrigin_Lua, uri, bodyData, bodySize) : 354 RequestOrigin_Lua, uri, bodyData, bodySize, headers) :
349 HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 355 HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
350 RequestOrigin_Lua, uri, bodyData, bodySize)) 356 RequestOrigin_Lua, uri, bodyData, bodySize, headers))
351 { 357 {
352 lua_pushlstring(state, result.c_str(), result.size()); 358 lua_pushlstring(state, result.c_str(), result.size());
353 return 1; 359 return 1;
354 } 360 }
355 } 361 }
389 return 1; 395 return 1;
390 } 396 }
391 397
392 // Check the types of the arguments 398 // Check the types of the arguments
393 int nArgs = lua_gettop(state); 399 int nArgs = lua_gettop(state);
394 if ((nArgs != 1 && nArgs != 2) || 400 if (nArgs < 1 || nArgs > 3 ||
395 !lua_isstring(state, 1) || // URI 401 !lua_isstring(state, 1) || // URI
396 (nArgs == 2 && !lua_isboolean(state, 2))) // Restrict to built-in API? 402 (nArgs >= 2 && !lua_isboolean(state, 2))) // Restrict to built-in API?
397 { 403 {
398 LOG(ERROR) << "Lua: Bad parameters to RestApiDelete()"; 404 LOG(ERROR) << "Lua: Bad parameters to RestApiDelete()";
399 lua_pushnil(state); 405 lua_pushnil(state);
400 return 1; 406 return 1;
401 } 407 }
402 408
403 const char* uri = lua_tostring(state, 1); 409 const char* uri = lua_tostring(state, 1);
404 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false); 410 bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) != 0 : false);
405 411
412 std::map<std::string, std::string> headers;
413 LuaContext::GetDictionaryArgument(headers, state, 3, true /* HTTP header key to lower case */);
414
406 try 415 try
407 { 416 {
408 if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 417 if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin),
409 RequestOrigin_Lua, uri)) 418 RequestOrigin_Lua, uri, headers))
410 { 419 {
411 lua_pushboolean(state, 1); 420 lua_pushboolean(state, 1);
412 return 1; 421 return 1;
413 } 422 }
414 } 423 }