/* Return raw file if found. Security risk?! Check of filename/path required?! */ int search_file(onion_dict *context, onion_request *req, onion_response *res){ //const char* path = onion_request_get_path(req);//empty?! const char* path = onion_request_get_fullpath(req); printf("Request of %s %i.\n",path, strlen(path)); char filename[strlen(path)+8]; //sprintf(filename,"./%s",path); sprintf(filename,"./html/%s",path); //read file if( FILE *f=fopen(filename,"rb") ){ fseek(f,0,SEEK_END); long len=ftell(f); fseek(f,0,SEEK_SET); char *data=(char*)malloc(len+1); fread(data,1,len,f); fclose(f); if (context) onion_dict_add(context, "LANG", onion_request_get_language_code(req), OD_FREE_VALUE); onion_response_set_length(res, len); onion_response_write(res, data, len); if (context) onion_dict_free(context); free(data); }else{ onion_response_set_length(res, 24); onion_response_write(res, "<h1>File not found</h1>", 24); } return OCS_PROCESSED; }
/* Like 'search_file' */ onion_connection_status OnionServer::getSettingKinectWrapped( Onion::Request &req, Onion::Response &res) { const char* path = onion_request_get_fullpath( req.c_handler() ); #ifdef VERBOSE printf("Request of %s.\n",path); #endif std::string filename("./html/"); filename.append(path); std::ifstream file(filename.c_str()); std::string line; if( file.is_open()){ /* Create header with mime type and charset information for several file extensions. * This is just a workaround. There should be an automatic mechanicm * in libonion. */ int periodPos = filename.find_last_of('.'); std::string extension = filename.substr(periodPos+1); std::string key("Content-Type"); std::string defaultType("text/html; charset: utf-8"); std::string mime = m_mimedict.get( extension , defaultType ) ; res.setHeader(key,mime); onion_response_write_headers(res.c_handler());// missing in cpp bindings? //res.writeHeaders();//this was added by me... try{ /* res.write("json_kinect = ", 14); const char* kinect = m_settingKinect.getConfig(true); size_t len = strlen( kinect ); res.write(kinect, (int) len ); res.write(";\n", 2 ); */ while (std::getline(file, line)) { res.write( line.c_str(), line.size() ); res.write("\n", 1 ); } }//catch ( const boost::iobase::failure &ex ) catch ( const std::exception & ex ){ std::cerr << "Can not read " << filename << std::endl; res.write( ErrReadFailed.c_str(), ErrReadFailed.size()); } }else{ res.write( ErrNotFound.c_str(), ErrNotFound.size()); } return OCS_PROCESSED; }
/** * @short Handles a propfind * * @param path the shared path. */ onion_connection_status onion_webdav_propfind(const char *filename, onion_webdav *wd, onion_request* req, onion_response* res){ // Prepare the basepath, necesary for props. char *basepath=NULL; int pathlen=0; const char *current_path=onion_request_get_path(req); const char *fullpath=onion_request_get_fullpath(req); pathlen=(current_path-fullpath); basepath=alloca(pathlen+1); memcpy(basepath, fullpath, pathlen+1); ONION_DEBUG0("Pathbase initial <%s> %d", basepath, pathlen); while(basepath[pathlen]=='/' && pathlen>0) pathlen--; basepath[pathlen+1]=0; ONION_DEBUG0("PROPFIND; pathbase %s", basepath); int depth; { const char *depths=onion_request_get_header(req, "Depth"); if (!depths){ ONION_ERROR("Missing Depth header on webdav request"); return OCS_INTERNAL_ERROR; } if (strcmp(depths,"infinity")==0){ ONION_ERROR("Infinity depth not supported yet."); return OCS_INTERNAL_ERROR; } depth=atoi(depths); } int props=onion_webdav_parse_propfind(onion_request_get_data(req)); ONION_DEBUG("Asking for props %08X, depth %d", props, depth); onion_block *block=onion_webdav_write_propfind(basepath, filename, onion_request_get_path(req), depth, props); if (!block) // No block, resource does not exist return onion_shortcut_response("Not found", HTTP_NOT_FOUND, req, res); ONION_DEBUG0("Printing block %s", onion_block_data(block)); onion_response_set_header(res, "Content-Type", "text/xml; charset=\"utf-8\""); onion_response_set_length(res, onion_block_size(block)); onion_response_set_code(res, HTTP_MULTI_STATUS); onion_response_write_headers(res); onion_response_flush(res); onion_response_write(res, onion_block_data(block), onion_block_size(block)); onion_block_free(block); return OCS_PROCESSED; }
/** * @short Moves a resource */ onion_connection_status onion_webdav_move(const char *filename, onion_webdav *wd, onion_request *req, onion_response *res){ const char *dest=onion_request_get_header(req,"Destination"); if (!dest) return OCS_INTERNAL_ERROR; const char *dest_orig=dest; // Skip the http... part. Just 3 /. int i; for (i=0;i<3;i+=(*dest++=='/')) if (*dest==0) return OCS_INTERNAL_ERROR; dest--; const char *fullpath=onion_request_get_fullpath(req); const char *partialpath=onion_request_get_path(req); // Not the fixed URL part for this handler. int fpl=strlen(fullpath); // Full path length int ppl=strlen(onion_request_get_path(req)); // Partial, the fullpath[fpl-ppl] is the end point of the handler path if (strncmp(fullpath, dest, fpl-ppl)!=0){ char tmp[512]; int l=fpl-ppl < sizeof(tmp)-1 ? fpl-ppl : sizeof(tmp)-1; strncpy(tmp, fullpath, l); tmp[l]=0; ONION_WARNING("Move to out of this webdav share! (%s is out of %s)", dest, tmp); return onion_shortcut_response("Moving out of shared share", HTTP_FORBIDDEN, req, res); } dest=&dest[fpl-ppl]; char orig[512]; snprintf(orig, sizeof(orig), "%s/%s", wd->path, partialpath); if (wd->check_permissions(wd->path, orig, req)!=0){ return onion_shortcut_response("Forbidden", HTTP_FORBIDDEN, req, res); } const char *fdest=filename; ONION_INFO("Move %s to %s (webdav)", fullpath, dest_orig); int ok=onion_shortcut_rename(orig, fdest); if (ok==0){ ONION_DEBUG("Created %s succesfully", fdest); return onion_shortcut_response("201 Created", 201, req, res); } else{ ONION_ERROR("Could not rename %s to %s (%s)", orig, fdest, strerror(errno)); return onion_shortcut_response("Could not create resource", HTTP_FORBIDDEN, req, res); } }
void momf_canvasedit (struct mom_item_st *tkitm) { enum canvedit_closoff_en { mec_wexitm, mec_protowebstate, mec_thiswebstate, mec__last }; struct mom_item_st *wexitm = NULL; struct mom_item_st *thistatitm = NULL; struct mom_item_st *sessitm = NULL; struct mom_item_st *hsetitm = NULL; mom_item_lock (tkitm); const struct mom_boxnode_st *tknod = (struct mom_boxnode_st *) tkitm->itm_payload; MOM_DEBUGPRINTF (web, "momf_canvedit start tkitm=%s tknod=%s", mom_item_cstring (tkitm), mom_value_cstring ((const struct mom_hashedvalue_st *) tknod)); if (mom_itype (tknod) != MOMITY_NODE || mom_raw_size (tknod) < mec__last) { /// should not happen MOM_WARNPRINTF ("momf_canvedit bad tknod %s", mom_value_cstring ((const struct mom_hashedvalue_st *) tknod)); goto end; } { wexitm = mom_dyncast_item (tknod->nod_sons[mec_wexitm]); assert (wexitm && wexitm->va_itype == MOMITY_ITEM); mom_item_lock (wexitm); thistatitm = mom_dyncast_item (tknod->nod_sons[mec_thiswebstate]); assert (thistatitm && thistatitm->va_itype == MOMITY_ITEM); MOM_DEBUGPRINTF (web, "momf_canvedit tkitm=%s wexitm=%s paylkind %s thistatitm=%s", mom_item_cstring (tkitm), mom_item_cstring (wexitm), mom_itype_str (wexitm->itm_payload), mom_item_cstring (thistatitm)); mom_item_lock (thistatitm); } struct mom_webexch_st *wexch = (struct mom_webexch_st *) wexitm->itm_payload; assert (wexch && wexch->va_itype == MOMITY_WEBEXCH); sessitm = wexch->webx_sessitm; mom_item_lock (sessitm); MOM_DEBUGPRINTF (web, "momf_canvedit tkitm=%s wexch #%ld meth %s fupath %s sessitm %s", mom_item_cstring (tkitm), wexch->webx_count, mom_webmethod_name (wexch->webx_meth), onion_request_get_fullpath (wexch->webx_requ), mom_item_cstring (sessitm)); sessitm->itm_pcomp = mom_vectvaldata_resize (sessitm->itm_pcomp, mes__last); if (! (hsetitm = mom_dyncast_item (mom_vectvaldata_nth (sessitm->itm_pcomp, mes_itmhset)))) { hsetitm = mom_clone_item (MOM_PREDEFITM (hashset)); mom_vectvaldata_put_nth (sessitm->itm_pcomp, mes_itmhset, (struct mom_hashedvalue_st *) hsetitm); hsetitm->itm_payload = (struct mom_anyvalue_st *) mom_hashset_reserve (NULL, 20); MOM_DEBUGPRINTF (web, "momf_canvedit tkitm=%s wexch #%ld new hsetitm %s", mom_item_cstring (tkitm), wexch->webx_count, mom_item_cstring (hsetitm)); } else MOM_DEBUGPRINTF (web, "momf_canvedit tkitm=%s wexch #%ld got hsetitm %s", mom_item_cstring (tkitm), wexch->webx_count, mom_item_cstring (hsetitm)); mom_item_lock (hsetitm); if (wexch->webx_meth == MOMWEBM_POST) { const char *dofillcanvas = onion_request_get_post (wexch->webx_requ, "do_fillcanvas"); MOM_DEBUGPRINTF (web, "momf_canvedit tkitm=%s wexch #%ld do_fillcanvas %s", mom_item_cstring (tkitm), wexch->webx_count, dofillcanvas); if (dofillcanvas) dofillcanvas_canvedit_mom (wexch, tkitm, wexitm, thistatitm); } end: if (hsetitm) mom_item_unlock (hsetitm); if (thistatitm) mom_item_unlock (thistatitm); if (wexitm) mom_item_unlock (wexitm); if (sessitm) mom_item_unlock (sessitm); mom_item_unlock (tkitm); } /* end of momf_canvedit */