Hybrid code generation method, device, computer equipment and storage medium

Through the hybrid code generation method, the identifiers of preset back-end languages ​​and front-end languages ​​are used to automatically generate complex codes that realize front-end interaction and web page rendering, solving the problem of high programming error rate in web application development and improving programming efficiency.

CN119002880BActive Publication Date: 2025-03-21ZHUOZHUO TECH
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411450129.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-17
Publication Date
2025-03-21
Estimated Expiration
2044-10-17

AI Technical Summary

Technical Problem

In web application development, when interacting front-end and rendering front-end pages, a large amount of back-end code is required to be written and the front-end code is continuously output, resulting in a high programming error rate.

Method used

It provides a hybrid code generation method. By obtaining the initial hybrid code, it is written mixed by preset backend language, preset front-end language and identifiers, extracts code blocks and generates target code blocks, and automatically generates complex codes that realize front-end interaction and web page rendering.

Benefits of technology

Reduces the workload of programmers, improves programming efficiency, and reduces the error rate due to mixed programming in multiple languages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119002880B_ABST
    Figure CN119002880B_ABST
Patent Text Reader

Abstract

The present application relates to a hybrid code generation method, device, computer equipment and storage medium, and belongs to the field of computer technology. The method includes obtaining an initial hybrid code, which is written by mixing a preset backend language, a preset front-end language and an identifier; extracting a number of initial code blocks from the initial hybrid code according to the identifier, and the identifier is used to wrap different types of initial code blocks; according to the type of the initial code block, generating a target code block of the preset backend language corresponding to the initial code block, and obtaining a target hybrid code according to the number of target code blocks. The present application is based on the ability to automatically generate complex code that can realize front-end and back-end interaction and web page rendering based on a simple code that is written by mixing a preset backend language, a preset front-end language and an identifier, which can greatly reduce the workload of programmers and improve programming efficiency, while avoiding the problem of easy errors in manual mixed programming using multiple programming languages.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a hybrid code generation method, apparatus, computer device and storage medium. Background Art

[0002] When developing Web (World Wide Web) applications, the front-end and back-end usually need to interact, and multiple languages ​​are used for mixed programming. For example, in mixed programming, back-end codes such as Golang, JAVA, and Python are written to output front-end codes such as HTML (Hyper Text Markup Language), CSS (Cascading Style Sheets), and JS (JavaScript) to achieve front-end and back-end interaction and web page rendering.

[0003] However, the above method requires writing a large amount of backend code, and during the writing process, it is necessary to continuously output the frontend code, resulting in a high programming error rate. Summary of the invention

[0004] In order to solve the above technical problems, the present application provides a hybrid code generation method, apparatus, computer device and storage medium, which can automatically generate complex code that can realize front-end and back-end interaction and web page rendering based on a simple code written in a mixture of a preset back-end language, a preset front-end language and an identifier.

[0005] In a first aspect, the present application provides a hybrid code generation method, the hybrid code generation method comprising:

[0006] Acquire an initial mixed code, where the initial mixed code is written by mixing a preset backend language, a preset frontend language, and an identifier;

[0007] Extracting a plurality of initial code blocks from the initial mixed code according to the identifier, wherein the identifier is used to wrap the initial code blocks of different types;

[0008] According to the type of the initial code block, a target code block corresponding to the preset backend language of the initial code block is generated, and a target mixed code is obtained according to a plurality of the target code blocks.

[0009] In one embodiment, the initial code blocks are divided into front-end code blocks, conventional back-end code blocks, statically assigned code blocks and dynamically assigned code blocks according to the types;

[0010] The step of generating a target code block corresponding to the preset backend language of the initial code block comprises:

[0011] Generate a first target code block corresponding to the front-end code block, wherein the first target code block includes code that calls a preset output function to output the front-end code block;

[0012] Generate a second target code block corresponding to the conventional backend code block, where the second target code block is the original code of the conventional backend code block;

[0013] Generate a third target code block corresponding to the statically assigned code block, wherein the third target code block includes code for calling the preset output function to output the statically assigned code block;

[0014] Generate a fourth target code block corresponding to the dynamically assigned code block, the fourth target code block includes code for calling the preset output function and the preset translation function corresponding to the dynamically assigned code block for output, the preset translation function is used to perform security processing on the dynamically assigned code block.

[0015] In one embodiment, before the step of generating a target code block corresponding to the preset backend language of the initial code block, the method further includes:

[0016] Determine the front-end code type where the dynamically assigned code block is located according to the code block to be analyzed; wherein the code block to be analyzed is the front-end code block located before the dynamically assigned code block;

[0017] The preset translation function is determined according to the front-end code type.

[0018] In one embodiment, the step of determining the front-end code type of the dynamically assigned code block according to the code block to be analyzed includes:

[0019] Read the characters in the code block to be analyzed in sequence, and set the internal state in the state machine according to the characters;

[0020] The front-end code type is determined according to the internal state and preset conditions.

[0021] In one embodiment, the internal state includes a first state parameter, a second state parameter, and a third state parameter;

[0022] The first state parameter is used to determine whether the next character to be read is inside the Script tag, the second state parameter is used to determine whether the next character to be read is between the Script start tag and the Script end tag, and the third state parameter is used to determine whether the next character to be read is in the URL query part;

[0023] The initial values of the first state parameter and the second state parameter are "false", and the initial value of the third state parameter is "not";

[0024] The step of setting the internal state in the state machine according to the character includes:

[0025] If "<script" is read, set the first state parameter to "true", and when the next ">" is read, set the first state parameter to "false" and set the second state parameter to "true";

[0026] If "" is read, set the second state parameter to "false";

[0027] If "?" is read, set the third state parameter to "question";

[0028] When the third state parameter is "question", if an allowed encoded string in the URL query part is read, set the third state parameter to "key";

[0029] When the third state parameter is "key", if "=" is read, set the third state parameter to "equal";

[0030] When the third state parameter is "equal", if the allowed encoded string in the URL query part is read, set the third state parameter to "value";

[0031] When the third state parameter is "value", if "&" is read, set the third state parameter to "andSign";

[0032] When the third state parameter is "andSign", if the allowed encoded string in the URL query part is read, set the third state parameter to "key";

[0033] If a non-allowed encoded string in the URL query part is read, set the third state parameter to "not";

[0034] The step of determining the front-end code type according to the internal state and preset conditions includes:

[0035] In response to the first state parameter being "false" and the second state parameter being "true", determine that the front-end code type is the Script code part;

[0036] In response to the third state parameter being "question", "equal", "key" or "value", determine that the front-end code type is the URL query part;

[0037] If the internal state cannot satisfy the preset condition, it is determined that the front-end code type is a regular HTML code portion.

[0038] In one embodiment, the step of extracting a plurality of initial code blocks from the initial mixed code according to the identifier comprises:

[0039] Extracting a plurality of initial code blocks from the initial mixed code in sequence according to the identifier;

[0040] Writing the initial code blocks and their corresponding type identifiers into a preset array in sequence according to the extraction order of the initial code blocks;

[0041] The step of generating a target code block of the preset backend language corresponding to the initial code block and obtaining a target mixed code according to a plurality of the target code blocks comprises:

[0042] Generate the target code blocks corresponding to the initial code blocks in sequence according to the preset array;

[0043] The target mixed code is obtained by combining the target code blocks in the generation order.

[0044] In one embodiment, the identifier includes a front identifier, a back identifier, an expression identifier, and a statically assigned identifier;

[0045] The step of extracting a plurality of initial code blocks from the initial mixed code according to the identifier comprises:

[0046] Read the initial mixed code in sequence, and when the preceding identifier is read, determine whether the preceding identifier is followed by an expression identifier;

[0047] If not, extracting the regular backend code block according to the initial mixed code between the front identifier and the first back identifier after it;

[0048] If so, determine whether the expression identifier is immediately followed by the static assignment identifier;

[0049] If not, extracting a dynamically assigned code block according to the initial mixed code between the expression identifier and the first subsequent identifier;

[0050] If yes, extracting a dynamically assigned code block according to the initial mixed code in the statically assigned identifier;

[0051] A front-end code block is extracted based on the initial mixed code between the rear identifier and the first front identifier after it.

[0052] In a second aspect, the present application provides a hybrid code generation device, the hybrid code generation device comprising:

[0053] An acquisition module, used for acquiring an initial mixed code, wherein the initial mixed code is written by mixing a preset backend language, a preset frontend language and an identifier;

[0054] A code extraction module, used for extracting a plurality of initial code blocks from the initial mixed code according to the identifier, wherein the identifier is used for wrapping the initial code blocks of different types;

[0055] The code generation module is used to generate a target code block corresponding to the preset backend language of the initial code block according to the type of the initial code block, and obtain a target mixed code according to a plurality of the target code blocks.

[0056] In a third aspect, the present application provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the steps of the hybrid code generation method described in the first aspect are implemented.

[0057] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the hybrid code generation method described in the first aspect are implemented.

[0058] On the basis of conforming to the common sense in the art, the above-mentioned preferred conditions can be arbitrarily combined to obtain the preferred embodiments of the present application.

[0059] The above-mentioned hybrid code generation method, device, computer equipment and storage medium can achieve the following beneficial effects: based on a simple code written in a mixture of a preset back-end language, a preset front-end language and an identifier, a number of initial code blocks are extracted from it according to the identifier, and according to the type of the initial code block, a target code block corresponding to the initial code block is automatically generated based on the preset back-end language, and a more complex target hybrid code is obtained based on the generated target code blocks, so as to replace the process of writing hybrid codes in multiple languages, which can greatly reduce the workload of programmers and improve programming efficiency, while avoiding the problem of easy errors in manual hybrid programming using multiple programming languages. BRIEF DESCRIPTION OF THE DRAWINGS

[0060] Figure 1 A schematic diagram of the flow of a hybrid code generation method in an embodiment of the present application;

[0061] Figure 2 This is a module diagram of a hybrid code generation device in an embodiment of the present application;

[0062] Figure 3 Schematic diagram of the internal structure of a computer device in an embodiment of the present application. DETAILED DESCRIPTION

[0063] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0064] It should be noted that the diagrams provided in the present embodiment are only used to illustrate the basic concept of the present application in a schematic manner. The structures, proportions, sizes, etc. illustrated by the drawings of the present specification are only used to match the contents disclosed in the specification, so that people familiar with this technology can understand and read them, and are not used to limit the restrictive conditions that can be implemented in the present application, so they have no technical substantive significance. Any modification of the structure, change of the proportional relationship, or adjustment of the size, without affecting the effects that can be produced by the present application and the purposes that can be achieved, should still fall within the scope that the technical content disclosed in the present application can cover. At the same time, the terms such as "upper", "lower", "left", "right", "middle", and "one" quoted in the present specification are also only for the convenience of narration, rather than for limiting the scope that can be implemented in the present application. The change or adjustment of the relative relationship, without substantial changes in the technical content, should also be regarded as the scope that can be implemented in the present application.

[0065] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various places in the text does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.

[0066] As shown in this document, unless the context clearly indicates an exception, the words "a", "an", "an" and / or "the" do not refer to the singular and may also include the plural, unless the context clearly indicates an exception. Generally speaking, the terms "include" and "comprise" only indicate that the steps and elements that have been clearly identified are included, and these steps and elements do not constitute an exclusive list. The method or device may also include other steps or elements.

[0067] The definitions of inclusion in this document, such as the terms "having", "may have", "including" or "may include" used herein, indicate the existence of the corresponding functions, operations, elements, etc. herein, and do not limit the existence of one or more other functions, operations, elements, etc. In addition, it should be understood that the terms "including" or "having" used herein indicate the existence of the features, numbers, steps, operations, elements, components, or a combination thereof described in the specification, and do not exclude the existence or addition of one or more other features, numbers, steps, operations, elements, components, or a combination thereof.

[0068] In the embodiments of the present application, prefixes such as "first" and "second" are used only to distinguish different description objects, and have no limiting effect on the position, order, priority, quantity or content of the described objects. The use of prefixes such as ordinal numbers to distinguish description objects in the embodiments of the present application does not constitute a limitation on the described objects. For the statement of the described objects, please refer to the description in the context of the claims or embodiments, and no unnecessary limitation should be constituted due to the use of such prefixes. In addition, in the description of the present embodiment, unless otherwise specified, the meaning of "multiple" is two or more.

[0069] In one embodiment, Figure 1 As shown, the hybrid code generation method provided by the present application includes the following steps:

[0070] S101, obtaining an initial mixed code;

[0071] S102, extracting a plurality of initial code blocks from the initial mixed code according to the identifier;

[0072] S103: Generate a target code block corresponding to the preset backend language of the initial code block according to the type of the initial code block, and obtain a target mixed code according to a plurality of the target code blocks.

[0073] The initial mixed code is written by mixing a preset backend language, a preset frontend language and an identifier, and the identifier is used to wrap different types of initial code blocks.

[0074] The user only needs to write a simple initial mixed code and use identifiers to distinguish different types of initial code blocks, which can be automatically converted into a complex target mixed code based on this embodiment.

[0075] In one embodiment, the step of obtaining the target mixed code according to the target code blocks includes: first creating a blank file to store the target mixed code, and then writing the target code blocks corresponding to the initial code blocks into the file.

[0076] If the initial mixed code includes functions written in a preset backend language, a preset frontend language and an identifier, it can be converted into a target mixed code corresponding to the preset backend language and written into the above file through the above embodiment. When performing front-end and back-end interaction and web page rendering, the above function can be directly called based on the above file.

[0077] In one embodiment, step S102 includes:

[0078] S1021. Extracting a plurality of initial code blocks from the initial mixed code in sequence according to the identifier;

[0079] S1022. Write the initial code blocks and their corresponding type identifiers into a preset array in sequence according to the extraction order of the initial code blocks.

[0080] Accordingly, step S103 includes:

[0081] Generate the target code blocks corresponding to the initial code blocks in sequence according to the preset array;

[0082] The target mixed code is obtained by combining the target code blocks in the generation order.

[0083] Exemplarily, step S1022 may specifically include:

[0084] Generate corresponding objects based on the plurality of initial code blocks respectively, wherein the objects are used to store the initial code blocks and their type identifiers;

[0085] According to the extraction order of the initial code blocks, the objects corresponding to the initial code blocks are written into the preset array in sequence.

[0086] Correspondingly, the step of sequentially generating the target code blocks corresponding to the initial code blocks according to the preset array includes:

[0087] The objects in the preset array are obtained in sequence, and a target code block corresponding to the initial code block is generated according to the type identifier in the object.

[0088] In one embodiment, the identifiers include a pre-identifier, a post-identifier, an expression identifier, and a statically assigned identifier.

[0089] Based on this, the specific process of extracting several initial code blocks includes:

[0090] Read the initial mixed code in sequence, and when the preceding identifier is read, determine whether the preceding identifier is followed by an expression identifier;

[0091] Otherwise, extract the regular backend code block according to the initial mixed code between the front identifier and the first subsequent identifier;

[0092] If so, determine whether the static assignment identifier immediately follows the expression identifier;

[0093] Otherwise, extract the dynamic assignment code block according to the initial mixed code between the expression identifier and the first subsequent identifier;

[0094] If so, extract the dynamic assignment code block according to the initial mixed code in the static assignment identifier;

[0095] Extract the front-end code block according to the initial mixed code between the subsequent identifier and the first subsequent front identifier.

[0096] Exemplarily, the front identifier is "<?", the corresponding subsequent identifier is "?>", the "=" immediately following the front identifier is the expression identifier, and the "raw()" static assignment identifier immediately following the expression identifier. That is, "<? " and "?>" are used to wrap the regular backend code block, "<?= " and "?>" are used to include the dynamic assignment code block, and the "(" and ")" in "<?=raw()" are used to include the static assignment code block.

[0097] Taking the following initial mixed code as an example:

[0098] <?

[0099] package example

[0100] / / xss test

[0101] func tplTest4()string{

[0102] a:="http: / / www.baidu.com / "

[0103] html:=" "

[0104] ?>

[0105] <a href="<?=a?>"><?=a?>

[0106] <?=raw(html)?>

[0107] <?}

[0108] ?>

[0109] According to the above-mentioned front identifier, back identifier, expression identifier and static assignment identifier, the above-mentioned initial mixed code is extracted, and the extraction result is shown in Table 1:

[0110] Table 1 Initial mixed code example 1

[0111]

[0112] The initial code blocks can be divided into front-end code blocks, conventional back-end code blocks, statically assigned code blocks and dynamically assigned code blocks according to the types, and different types of initial code blocks need to generate different codes.

[0113] In one embodiment, the step of generating a target code block corresponding to the preset backend language of the initial code block includes:

[0114] Generate a first target code block corresponding to the front-end code block, wherein the first target code block includes code that calls a preset output function to output the front-end code block;

[0115] Generate a second target code block corresponding to the conventional backend code block, where the second target code block is the original code of the conventional backend code block;

[0116] Generate a third target code block corresponding to the statically assigned code block, wherein the third target code block includes code for calling the preset output function to output the statically assigned code block;

[0117] Generate a fourth target code block corresponding to the dynamically assigned code block, the fourth target code block includes code for calling the preset output function and the preset translation function corresponding to the dynamically assigned code block for output, the preset translation function is used to perform security processing on the dynamically assigned code block.

[0118] In this embodiment, considering the related issues of network security, the above-mentioned dynamically assigned code block is prone to XSS (Cross-Site Scripting) vulnerabilities when implementing front-end and back-end interactions. Attackers can exploit this vulnerability to execute malicious scripts in the browser to illegally steal user information, impersonate user operations, redirect to malicious websites, etc.

[0119] Therefore, for a statically assigned code block, you only need to generate code based on the preset backend language to call the preset output function to output the statically assigned code block, and you can get the required target code block. For a dynamically assigned code block, in addition to calling the preset output function, you also need to first call the preset translation function to perform security processing on the dynamically assigned code block. The specific security processing process in the preset translation function can be designed according to actual needs, and then call the preset output function to output the processed dynamically assigned code block.

[0120] The preset translation function can be designed for security processing according to actual needs. For example, different preset translation functions can be designed for different front-end code types where the dynamic assignment code block is located.

[0121] In one embodiment, before step S103, the method further includes:

[0122] S1023, determining the front-end code type of the dynamically assigned code block according to the code block to be analyzed; wherein the code block to be analyzed is the front-end code block located before the dynamically assigned code block;

[0123] S1024. Determine the preset translation function according to the front-end code type.

[0124] In this embodiment, the front-end code type of the dynamically assigned code block can be analyzed based on the front-end code block located before the dynamically assigned code block, such as the Script code part, HTML text part and URL query part, and the corresponding preset translation function can be selected according to the front-end code type.

[0125] In one embodiment, step S1023 specifically includes:

[0126] Read the characters in the code block to be analyzed in sequence, and set the internal state in the state machine according to the characters;

[0127] The front-end code type is determined according to the internal state and preset conditions.

[0128] Among them, the state machine, also known as the finite state automaton, is a mathematical computational model that represents a finite number of states and behaviors such as transitions and actions between these states.

[0129] Exemplarily, the internal state of the state machine includes a first state parameter, a second state parameter, and a third state parameter.

[0130] Among them, the first status parameter is used to determine whether the next character to be read is inside the tag of the <Script> tag, the second status parameter is used to determine whether the next character to be read is between the start tag and the end tag of <Script>, and the third status parameter is used to determine whether the next character to be read is in the URL query part.

[0131] The initial values of the first status parameter and the second status parameter are "false", and the initial value of the third status parameter is "not".

[0132] The preset condition 1 is that the first status parameter is "false" and the second status parameter is "true". The preset condition 2 is that the third status parameter is "question", "equal", "key", or "value".

[0133] Based on this, the step of setting the internal state in the state machine according to the character includes:

[0134] If "<script" is read, set the first status parameter to "true", and when the next ">" is read, set the first status parameter to "false" and set the second status parameter to "true";

[0135] If "" is read, set the second status parameter to "false";

[0136] If "?" is read, set the third status parameter to "question";

[0137] When the third status parameter is "question", if an allowed encoded string in the URL query part is read, set the third status parameter to "key";

[0138] When the third status parameter is "key", if "=" is read, set the third status parameter to "equal";

[0139] When the third status parameter is "equal", if the allowed encoded string in the URL query part is read, set the third status parameter to "value";

[0140] When the third status parameter is "value", if "&" is read, set the third status parameter to "andSign";

[0141] When the third status parameter is "andSign", if the allowed encoded string in the URL query part is read, set the third status parameter to "key";

[0142] If a character string that is not allowed to be encoded is read in the URL query part, the third status parameter is set to "not".

[0143] Among them, uppercase letters, lowercase letters, numbers 0-9, "-", ".", and "_" are allowed encoding strings in the URL query part and do not need to be translated. Other strings are not allowed encoding strings in the URL query part.

[0144] The step of determining the front-end code type according to the internal state and the preset condition comprises:

[0145] In response to the first state parameter being "false" and the second state parameter being "true", determining that the front-end code type is a Script code portion;

[0146] In response to the third status parameter being "question", "equal", "key" or "value", determining that the front-end code type is a URL query part;

[0147] If the internal state cannot satisfy the preset condition, it is determined that the front-end code type is a regular HTML code portion.

[0148] Accordingly, step S1024 specifically includes:

[0149] If the front-end code type of the dynamically assigned code block is the Script code part, the JSON serialization function is used as the preset translation function of the dynamically assigned code block. If the front-end code type of the dynamically assigned code block is the URL query part, the URL query translation function is used as the preset translation function of the dynamically assigned code block. If the front-end code type of the dynamically assigned code block is the HTML regular code part, the front-end regular code translation function is used as the preset translation function of the dynamically assigned code block.

[0150] Among them, the front-end regular code translation function is used to perform security processing on dynamically assigned code blocks to prevent HTML injection attacks and ensure that the text inserted into the HTML document will not be interpreted as HTML code by the browser. For example, special HTML characters (such as "<", ">", "&", """, "'") are translated into their HTML entity codes (such as , , , , ).

[0151] The URL query translation function is mainly used to perform security processing on dynamically assigned code blocks, ensuring that the query parameters in the URL query part maintain their meaning during transmission, and avoiding parsing errors caused by special characters, which may lead to XSS vulnerabilities. For example, uppercase letters, lowercase letters, numbers 0-9, "-", ".", and "_" are allowed encoding strings in the URL query part and do not need to be translated. Other strings are not allowed encoding strings in the URL query part and need to be translated into URL encoding form.

[0152] The JSON serialization function is mainly used to perform JSON serialization on dynamically assigned code blocks, ensuring that the JSON data maintains its structure and meaning during serialization and deserialization, and avoiding parsing errors caused by special characters. For example, frequently used control characters and special characters (such as double quotes, backslashes, control characters, etc.) in JSON strings are translated to meet the requirements of the JSON format.

[0153] In addition, for the necessary import information when the preset backend language is running, the code corresponding to the import information can be directly written in the initial mixed code as a regular backend code block and wrapped with an identifier. The corresponding preset code block can also be generated according to the import information. After obtaining the target mixed code, the preset code block can be inserted into the specified position in the target mixed code.

[0154] Similarly, based on the preset backend language, the necessary declaration information for calling the preset output function, such as the buffer object, can be written directly in the initial mixed code and wrapped with an identifier. Alternatively, the corresponding preset code block can be generated based on the declaration information. After obtaining the target mixed code, the preset code block can be inserted into the specified position in the target mixed code.

[0155] It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.

[0156] Corresponding to the aforementioned hybrid code generation method, the present application also provides a hybrid code generation device.

[0157] In one embodiment, Figure 2 As shown, the above-mentioned mixed code generating device includes:

[0158] An acquisition module 201 is used for acquiring an initial mixed code, where the initial mixed code is written by mixing a preset backend language, a preset frontend language and an identifier;

[0159] A code extraction module 202, configured to extract a plurality of initial code blocks from the initial mixed code according to the identifier, wherein the identifier is used to wrap different types of the initial code blocks;

[0160] The code generation module 203 is used to generate a target code block corresponding to the preset backend language of the initial code block according to the type of the initial code block, and obtain a target mixed code according to a plurality of the target code blocks.

[0161] For the specific definition of the hybrid code generation device, please refer to the definition of the hybrid code generation method above, which will not be repeated here. Each module in the above hybrid code generation device can be implemented in whole or in part by software, hardware and a combination thereof. Each of the above modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to each of the above modules.

[0162] The implementation process of the above hybrid code generation method and device is described below through a specific example. Take the initial hybrid code shown below as an example:

[0163] <?

[0164] package example

[0165] import (

[0166] "bytes"

[0167] "myLib / myXss" )

[0169] func tplTest4()string{

[0170] a:="http: / / www.baidu.com / "

[0171] html:=" "

[0172] var _buf bytes.Buffer

[0173] ?>

[0174] <a href="<?=raw(html)?>?v1=5&v2=<?=a?>"><?=a?>

[0175] <script>

[0176] var jsA =<?=a?>;

[0177] < / script>

[0178] <? return _buf.String()

[0179] }

[0180] ?>

[0181] In the above initial mixed code, the front identifier is "<?", and the corresponding back identifier is always "?>". The "=" immediately following the front identifier is the expression identifier, and the "raw()" immediately following the expression identifier is the static assignment identifier. That is, "<?" and "?>" are used to wrap regular backend code blocks, "<?=" and "?>" are used to enclose dynamic assignment code blocks, and the "(" and ")" in "<?=raw()?>" are used to wrap static assignment code blocks.

[0182] Obtain the file containing the initial mixed code, read the characters in the initial mixed code in the file sequentially from beginning to end, extract multiple initial code blocks, and write the initial code blocks and their type identifiers into a preset array in the extraction order.

[0183] Among them, the initial code blocks can be divided into front-end code blocks and back-end code blocks according to their types. The back-end code blocks can be further divided into regular back-end code blocks, static assignment code blocks, and dynamic assignment code blocks.

[0184] During the process of extracting the initial code blocks, the types of the initial code blocks can be determined according to the above identifiers.

[0185] The specific extraction process is as follows:

[0186] When the front identifier "<? " is read, it indicates that the initial code block following it is a back-end code block.

[0187] If the expression identifier "=" is not immediately following the front identifier "<?", the initial code block between the front identifier "<? " and the next back identifier "?>" can be extracted to obtain a regular back-end code block, and the regular back-end code block and its type identifier are correspondingly written into the array. For example, an object for storing the initial code blocks can be created based on it, and the regular back-end code block and its type identifier are stored in the object for storing the back-end code blocks, and then the object is written into the array.

[0188] When the post-identifier "?>" is read, it indicates that the initial code block following it is a front-end code block. The initial code block between the post-identifier "?>" and the next pre-identifier "?>" can be extracted to obtain the front-end code block, and the front-end code block and its type identifier can be correspondingly written into an array. For example, an object can be generated based on it, and the front-end code block and its type identifier can be stored in the object, and then the object can be written into the array.

[0189] If the pre-identifier "<?", followed by the expression identifier "=" and the static assignment identifier "raw()" in sequence, the initial code block between "(" and ")" can be extracted to obtain the static assignment code block, and the static assignment code block and its type identifier can be correspondingly written into an array. Similarly, an object can be generated based on it, and the static assignment code block and its type identifier can be stored in the object, and then the object can be written into the array.

[0190] If the pre-identifier "<?", followed only by the expression identifier "=", the initial code block between the expression identifier "=" and the next post-identifier "?>" can be extracted to obtain the dynamic assignment code block, and the dynamic assignment code block, its type identifier, and the required preset translation function can be correspondingly written into an array. For example, an object can be generated based on it, and the dynamic assignment code block, its type identifier, and the required preset translation function can be stored in the object, and then the object can be written into the array.

[0191] Table 2 Initial Mixed Code Example 2

[0192]

[0193] Referring to the recognition results of the initial mixed code in Table 2, the conventional back-end code block 1, front-end code block 1, static assignment code block 1, front-end code block 2, dynamic assignment code block 1, front-end code block 3, dynamic assignment code block 2, front-end code block 4, dynamic assignment code block 3, front-end code block 5, and conventional back-end code block 2 can be sequentially extracted.

[0194] For the above dynamic assignment code block, the front-end code block before the dynamic assignment code block can be analyzed through a state machine to determine the front-end code type where the dynamic assignment code block is located, and then different security processing procedures can be selected, that is, the required preset translation function for the dynamic assignment code block can be determined.

[0195] The state machine can adopt three internal states: "isLastScriptToken", "isInScript", and "urlvStatus". Among them, "isLastScriptToken" is used to determine whether the next character to be read is inside the tag of the Script tag. "isInScript" is used to determine whether the next character to be read is between the start tag and the end tag of Script. "urlvStatus" is used to determine whether the next character to be read is in the URL query part. The initial values of "isLastScriptToken" and "isInScript" are "false", and the initial value of "urlvStatus" is "not".

[0196] The principle of determining the preset translation function required for the dynamic assignment code block using the above internal states of the state machine is as follows:

[0197] If "<script" is read, set "isLastScriptToken" to "true", and when the next ">" is read, set "isLastScriptToken" to the first state parameter to "false", and set "isInScript" to "true";

[0198] If "" is read, set "isInScript" to "false";

[0199] If "?" is read, set "urlvStatus" to "question";

[0200] When "urlvStatus" is "question", if an allowed encoded string in the URL query part is read, set "urlvStatus" to "key";

[0201] When "urlvStatus" is "key", if "=" is read, set "urlvStatus" to "equal";

[0202] When "urlvStatus" is "equal", if the allowed encoded string in the URL query part is read, set "urlvStatus" to "value";

[0203] When "urlvStatus" is "value", if "&" is read, set "urlvStatus" to "andSign";

[0204] When "urlvStatus" is "andSign", if the allowed encoding string in the URL query part is read, set "urlvStatus" to "key";

[0205] If a string that does not allow encoding is read in the URL query part, set "urlvStatus" to "not".

[0206] Based on the changes in the internal state of the above state machine, when the dynamically assigned code block is extracted:

[0207] (1) If "isLastScriptToken" is "false" and "isInScript" is "true", it can be determined that the dynamically assigned code block is between the Script start tag and the Script end tag, and then it can be determined that the preset translation function of this dynamically assigned code block is myXss.Jsonv(), which is used to perform JSON serialization on the dynamically assigned code block between the Script start tag and the Script end tag.

[0208] (2) If "urlvStatus" is "question", "equal", "key" or "value", it can be determined that the dynamically assigned code block is in a specific tag, and then it can be determined that the preset translation function of this dynamically assigned code block is myXss.Urlv(), and myXss.Jsonv() is used to translate the dynamically assigned code block.

[0209] (3) For other situations, it can be determined that the dynamically assigned code block is in a general HTML text, that is, it is neither between the Script start tag and the Script end tag, nor in the URL query part. Then, it can be determined that the preset translation function of this dynamically assigned code block is myXss.H(), which is a function used to translate the dynamically assigned code block in a general HTML text.

[0210] Based on the above content, it can be determined that the preset translation function of the dynamic assignment code block 1 is the front-end regular code translation function myXss.H(), the preset translation function of the dynamic assignment code block 2 is the URL query translation function myXss.Urlv(), and the preset translation function of the dynamic assignment code block 3 is the JSON serialization function myXss.Jsonv().

[0211] Finally, according to the contents in the above array, the corresponding target code blocks are output in sequence to a pre-created file for storing the target mixed code.

[0212] The specific output results are shown in Table 3.

[0213] Table 3 Target code block example

[0214]

[0215] Referring to Table 3, currently, it is necessary to manually write complex mixed codes as shown in the target code block in Table 3, but with the mixed code generation method of the present application, it is only necessary to write a simple initial mixed code similar to the above as a template file, and the required target mixed code and files can be automatically generated based on the template file. This can greatly reduce the workload of programmers, improve programming efficiency, and avoid the problem of easy errors in manual mixed programming using multiple languages.

[0216] The present application also provides a computer device. In one embodiment, the computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and when the processor executes the computer program, the steps of the hybrid code generation method in the above embodiment are implemented.

[0217] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 3 As shown. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a hybrid code generation method is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.

[0218] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0219] The present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the hybrid code generation method in the above embodiment are implemented.

[0220] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0221] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0222] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A hybrid code generation method, characterized in that: The hybrid code generation method comprises: Acquire an initial mixed code, where the initial mixed code is written by mixing a preset backend language, a preset frontend language, and an identifier; Extracting a plurality of initial code blocks from the initial mixed code according to the identifier, wherein the identifier is used to wrap the initial code blocks of different types, and the initial code blocks are divided into front-end code blocks, conventional back-end code blocks, statically assigned code blocks and dynamically assigned code blocks according to the types; According to the type of the initial code block, a target code block corresponding to the preset backend language of the initial code block is generated, and a target mixed code is obtained according to a plurality of the target code blocks.

2. The hybrid code generation method according to claim 1, characterized in that: The step of generating a target code block corresponding to the preset backend language of the initial code block comprises: Generate a first target code block corresponding to the front-end code block, wherein the first target code block includes code that calls a preset output function to output the front-end code block; Generate a second target code block corresponding to the conventional backend code block, where the second target code block is the original code of the conventional backend code block; Generate a third target code block corresponding to the statically assigned code block, wherein the third target code block includes code for calling the preset output function to output the statically assigned code block; Generate a fourth target code block corresponding to the dynamically assigned code block, the fourth target code block includes code for calling the preset output function and the preset translation function corresponding to the dynamically assigned code block for output, the preset translation function is used to perform security processing on the dynamically assigned code block.

3. The hybrid code generation method according to claim 2, characterized in that: Before the step of generating a target code block corresponding to the preset backend language of the initial code block, the method further includes: Determine the front-end code type where the dynamically assigned code block is located according to the code block to be analyzed; wherein the code block to be analyzed is the front-end code block located before the dynamically assigned code block; The preset translation function is determined according to the front-end code type.

4. The hybrid code generation method according to claim 3, characterized in that: The step of determining the front-end code type of the dynamically assigned code block according to the code block to be analyzed comprises: Read the characters in the code block to be analyzed in sequence, and set the internal state in the state machine according to the characters; The front-end code type is determined according to the internal state and preset conditions.

5. The hybrid code generation method according to claim 4, characterized in that: The internal state includes a first state parameter, a second state parameter and a third state parameter; The first state parameter is used to determine whether the next character to be read is inside the Script tag, the second state parameter is used to determine whether the next character to be read is between the Script start tag and the Script end tag, and the third state parameter is used to determine whether the next character to be read is in the URL query part; The initial values ​​of the first state parameter and the second state parameter are "false", and the initial value of the third state parameter is "not"; The step of setting the internal state in the state machine according to the character comprises: If "<script” is read, set the first status parameter to "true”, and when the next ">" is read, set the first status parameter to "false” and the second status parameter to "true”; If "" is read, set the second status parameter to "false”; If "?” is read, set the third status parameter to "question”; When the third status parameter is "question”, if an allowed encoded string in the URL query part is read, set the third status parameter to "key”; When the third status parameter is "key”, if "=” is read, set the third status parameter to "equal”; When the third status parameter is "equal”, if the allowed encoded string in the URL query part is read, set the third status parameter to "value”; When the third status parameter is "value”, if "&” is read, set the third status parameter to "andSign”; When the third status parameter is "andSign”, if the allowed encoded string in the URL query part is read, set the third status parameter to "key”; If a disallowed encoded string in the URL query part is read, set the third status parameter to "not”; The step of determining the front - end code type according to the internal status and preset conditions includes: In response to the first status parameter being "false” and the second status parameter being "true”, determine that the front - end code type is the Script code part; In response to the third status parameter being "question”, "equal”, "key” or "value”, determine that the front - end code type is the URL query part; If the internal status cannot meet the preset conditions, determine that the front - end code type is the HTML normal code part.

6. The hybrid code generation method according to claim 1, characterized in that: The step of extracting a number of initial code blocks from the initial mixed code according to the identifier includes: Extract a number of initial code blocks from the initial mixed code in sequence according to the identifier; Write the initial code block and its corresponding type identifier into a preset array in the extraction order of the initial code block; The step of generating a target code block corresponding to the preset back - end language for the initial code block and obtaining a target mixed code according to a number of the target code blocks includes: Generate the target code blocks corresponding to the initial code blocks in sequence according to the preset array; Combine the target code blocks in the generation order to obtain the target mixed code.

7. The hybrid code generation method according to any one of claims 1 to 6, characterized in that: The identifier includes a front identifier, a back identifier, an expression identifier and a static assignment identifier; The step of extracting a number of initial code blocks from the initial mixed code according to the identifier includes: Read the initial mixed code in sequence. When the front identifier is read, determine whether the expression identifier immediately follows the front identifier; If not, extracting the regular backend code block according to the initial mixed code between the front identifier and the first back identifier after it; If so, determine whether the expression identifier is immediately followed by the static assignment identifier; If not, extracting a dynamically assigned code block according to the initial mixed code between the expression identifier and the first subsequent identifier; If yes, extracting a dynamically assigned code block according to the initial mixed code in the statically assigned identifier; A front-end code block is extracted based on the initial mixed code between the rear identifier and the first front identifier after it.

8. A hybrid code generating device, characterized in that: The hybrid code generating device comprises: An acquisition module, used for acquiring an initial mixed code, wherein the initial mixed code is written by mixing a preset backend language, a preset frontend language and an identifier; A code extraction module, used for extracting a plurality of initial code blocks from the initial mixed code according to the identifier, wherein the identifier is used for wrapping the initial code blocks of different types, and the initial code blocks are divided into front-end code blocks, conventional back-end code blocks, statically assigned code blocks and dynamically assigned code blocks according to the types; The code generation module is used to generate a target code block corresponding to the preset backend language of the initial code block according to the type of the initial code block, and obtain a target mixed code according to a plurality of the target code blocks.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Code generation method and device, equipment and storage medium

    CN115599386A